How to
get the last inserted row id from sqlite
:
I believe selecting from SQL_LITE_SEQUENCE will get the latest ID from
ANY table, you can also access the SQL_LITE_SEQUENCE by selecting ROWID on any
table, and getting just the IDs for that table
String
query = "SELECT ROWID from MYTABLE order by ROWID DESC limit 1";
Cursor c = db.rawQuery (query);
if (c != null && c.moveToFirst())
{
lastId = c.getLong(0);
//The 0 is the column index, we only have 1 column, so the index is 0
}
(Note that although the SQL Lite docs call ROWID an Integer, it is a 64
bit integer, so in Java it should be retrieved as a long.)
(or)
We can use
SELECT last_insert_rowid();
If you are using AUTOINCREMENT keyword then
SELECT * from SQLITE_SEQUENCE;
will tell you
the values for every table.
No comments:
Post a Comment