On iPhone, what's the best way to get the ID of the last inserted row on an SQLite Database using FMDB ?
Is there a better way rather than doing :
SELECT MAX(ID)
On iPhone, what's the best way to get the ID of the last inserted row on an SQLite Database using FMDB ?
Is there a better way rather than doing :
SELECT MAX(ID)
Try the following:
The function sqlite3_last_insert_rowid() is what you're looking for. Having just checked out the source code for FMDB, there seems to be a method on FMDatabase called
-lastInsertRowId
that wraps the function.If you can guarantee that your
ID
column is an auto-increment column,MAX(ID)
is fine.But to cover any case, there's a specialized SQLite function called
LAST_INSERT_ROWID()
:In FMDB, you use the
-lastInsertRowId
method (which internally runs the above):