Click here to Skip to main content
14,422,224 members
Rate this:
Please Sign up or sign in to vote.
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";
                
                conn.Open();
                cmd.ExecuteNonQuery();

<big>                _id = (int) cmd_id.ExecuteScalar(); <--- the code gags on this telling me I am not getting a valid value!!! 
</big>                //cmd_id.
                conn.Close();
                conn.Dispose();
                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)...

Thanks

Michael
Posted
Updated 15-Sep-19 6:37am
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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";
conn.Open();
cmd.ExecuteNonQuery();
System.Object temp = cmd_id.ExecuteScalar();
_id = int.Parse (temp.ToString());


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

Thanks
Michael
   
Comments
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..

Thanks..
Rate this:
Please Sign up or sign in to vote.

Solution 3

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!

Michael
   
Comments
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
Rate this:
Please Sign up or sign in to vote.

Solution 5

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100