Dear list,

After having made an SQLite statement the ID of a newly inserted row can be
retrieved with sqlite3_last_insert_rowid .

It'd be a great thing to be able to produce a general mechanism for
retrieving this value with regard to the most recently performed query only
right after a query has been made, as programming aimed at getting this
value lazily won't work as other local code may have made another query to
the SQLite handle meanwhile.

The most robust way to do this would be through having
a sqlite3_reset_last_insert_rowid() procedure to invoke right before a
query, because, sqlite3_last_insert_rowid is only updated on a successful
insert.

The code to check if a query is non-readonly can be done using
sqlite3_stmt_readonly , but then the step from there to check if it's an
*insert* and not only that but a successful insert, is a huge step and
possibly the only reliable way to tell this, would be by SQLite telling it,
and the most straightforward way for it to do this would be through
sqlite3_last_insert_rowid , so then what about un-problematizing that value
as to guarantee it won't return any obsolete value, by introducing a
sqlite3_reset_last_insert_rowid() ?

Please let me know the best practice for solving this particular problem -
the ability to make a "Query" abstraction atop SQLite, that has its very
own "ID of row inserted" method, I believe is a reasonable aim.

(I.e., not having such a property but that is not guaranteed to actually
contain the right thing, depending on very specific circumstances.)

Example:

(mutex for sqlite3 lock)
sqlite3_reset_last_insert_rowid(sqlite3*);
(perform SQLite query on sqlite3*)
sqlite3_last_insert_rowid(sqlite3*) => Guaranteedly returns the rowid of
any row inserted by the query, or 0 if no insert was done.
(mutex for sqlite3 unlock)
[starting here sqlite3_last_insert_rowid(sqlite3)'s return value is
undefined]



Thanks,
Mikael
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to