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)
9 Obtaining the Unique ID for the Last Inserted Row. If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
MySQL LAST_INSERT_ID() Function The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.
If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL. Insert some records in the table using insert command.
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()
:
SELECT LAST_INSERT_ROWID();
In FMDB, you use the -lastInsertRowId
method (which internally runs the above):
int lastId = [fmdb lastInsertRowId];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With