2

How to return auto generated column values from sqlite database in c#. I'm using Sqlite.data.dll.

Tried by executing query : "select last_insert_rowid() but it doesn;t seem to work as it is returning 0s. I'm using SqliteDataReader object to get the result.

Thanks

2 Answers2

1

I tried these commands, and it worked fine (even if the last_insert_rowid() call is from another transaction):

sqlite> create table mytable(pk integer primary key, other stuff);
sqlite> insert into mytable values(null, 42);
sqlite> select last_insert_rowid();
1

The only requirement of last_insert_rowid() is that it is called on the same database connection; please check that you didn't open another one.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • Thanks...I was using different database connection for getting the autogenerated column, but using the same connection it is working fine. – user1140366 Sep 05 '12 at 08:33
  • Is this threadsafe? What if a different connection has inserted between your second and third statement (between `insert` and `select last_ins`...)? – lmat - Reinstate Monica Mar 19 '13 at 16:28
  • @LimitedAtonement This is [documented](http://www.sqlite.org/c3ref/last_insert_rowid.html). – CL. Mar 19 '13 at 17:42
0

is your id is incremented? then try this:

SELECT max(id)

or you can refer to this post

Click HERE

Community
  • 1
  • 1
Patrick Guimalan
  • 990
  • 9
  • 11
  • ID column is autoincrement column and primary key. SELECT max(ID) works but is this the best way to retrieve the autogenerated column values? – user1140366 Sep 05 '12 at 04:47