Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ADO.NET Sqlite
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 22-May-11 9:35am
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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
  Permalink  
Comments
R. Hoffmann at 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 at 17-Sep-13 4:27am
   
Working code..
 
Thanks..
Rate this: bad
good
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
  Permalink  
Comments
R. Hoffmann at 23-May-11 11:07am
   
Cool, glad that's sorted :)

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

  Print Answers RSS
0 OriginalGriff 428
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100