Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I'm trying to bone up on C# for a job interview I have this coming week... thought I'd write a little application to manage my Tablature/Lyrics collection... should be pretty simple me thinks... I'm having problems retrieving the last insert id from an sqlite database... the insert IS working, but I cannot seem to get the follow up query to work... the code is below...

           String connString = "Data Source=" + Properties.Resources.dataSource;
                SQLiteConnection conn = new SQLiteConnection(connString);

                SQLiteCommand cmd = new SQLiteCommand(conn);
                cmd.CommandText = "insert into lyrics (song, lyrics) values (@title, @words);";
                cmd.Parameters.AddWithValue("@title", _title);
                cmd.Parameters.AddWithValue("@words", _words);
                SQLiteCommand cmd_id = new SQLiteCommand(conn);
                cmd_id.CommandText = "select last_insert_rowid() as id from lyrics";

<big>                _id = (int) cmd_id.ExecuteScalar(); <--- the code gags on this telling me I am not getting a valid value!!! 
</big>                //cmd_id.
                return true;

Any suggestions would be appreciated... I've been all over Google with this (which is where I got the code that I have)...


Updated 15-Sep-19 5:37am

How about assigning the return value from ExecuteScalar() to a temp variable of type Object, and then looking under a debugger to see what is being returned?

In other words:
object val = cmd_id.ExecuteScalar();
_id = (int)val; // Set a breakpoint here, and examine the contents of val

Maybe you're getting back a DbNull value, indicating that your query didn't execute properly. Or perhaps the value that is being returned by last_insert_rowid() isn't of type int.
Ok, thanks for that "object" suggestion. In fact I was getting and object back with the 'right' answer. Changed my code as below and things seem to be working...

cmd_id.CommandText = "select last_insert_rowid() as id from lyrics";
System.Object temp = cmd_id.ExecuteScalar();
_id = int.Parse (temp.ToString());

Is there a 'neater','cleaner' way to do that conversion?

R. Hoffmann 22-May-11 17:13pm
It depends: if you're getting back a string from ExecuteScalar, then you're not going to get it much neater than what you have now. You could get rid of the temp variable, and put cmd_id.ExecuteScalar().ToString() into the Parse call directly, but that's it. You might want to add code to handle DBNulls though, for when there is no last ID to retrieve.

However, have a look at what the type of the return value actually is (under the debugger). Maybe you can still use your original code, but with the correct type (i.e. not int). Maybe it's of type uint or something. E.g. _id = (uint) cmd_id.ExecuteScalar();
harshadcse 17-Sep-13 4:27am
Working code..

The debugger shows me getting a long back. Hmmm... guess I need to revisit the underlying class and change the _id from an int to a long. The Sqlite table definition is 'INTEGER'... Suppose I need to look at the Sqlite documentation to see what that actually means.

Thanks for the input!

R. Hoffmann 23-May-11 11:07am
Cool, glad that's sorted :)
Member 11631592 17-May-15 11:43am
its returning 0 in _id help please
The problem is: you think last_insert_rowid() is returning the information from your INTEGER PRIMARY KEY column, it's not it's returning the value in the hidden field rowid. You can see this information if you query the table "SELECT rowid, * FROM your_table".

So to really get the PRIMARY KEY field you should query for that. (this also helps if you aren't using an INTEGER .. ie a licence plate)

SELECT my_primary_key FROM my_table
WHERE rowid in (select DISTINCT (last_insert_rowid()) FROM my_table);

last_insert_rowid() will return a record set with x records (x = number of rows in the table) so distinct makes it a bit faster.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900