On 9 Apr 2012, at 2:14am, Stephan Beal <[email protected]> wrote:

> Is it reliable/portable/well-defined to use (read-only) the sqlite_sequence
> table to find the last insertion ID for a given table? i know about
> sqlite3_last_insert_id(), but i'm wondering if reading the sqlite_sequence
> table directly might be safer vis-a-vis triggers which perform an insert
> (and thereby cause sqlite3_last_insert_id() to possibly return an
> unexpected value).

The C function sqlite3_last_insert_id(), and the SQLite wrapper around it 
last_insert_rowid() are as good as it gets.  You do have to be a little careful 
if you have a TRIGGER on an INSERT which does another INSERT.  If you use 
TRIGGERs I suggest you test out the behaviour for yourself to figure out what 
it does.

There is no documentation about exactly when the SQLITE_SEQUENCE table is 
updated.  You may find unexpected circumstances if you look at that table 
inside a transaction.

> Take from a db dump:
> 
> DELETE FROM sqlite_sequence;
> INSERT INTO "sqlite_sequence" VALUES('t',8);

See the last section of

<http://www.sqlite.org/autoinc.html>

Simon.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to