Best way to get the ID of the last inserted row on

2019-01-22 12:26发布

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)

3条回答
走好不送
2楼-- · 2019-01-22 13:13

Try the following:

var rowid: Int = Int(contactDB.lastInsertRowId())
查看更多
可以哭但决不认输i
3楼-- · 2019-01-22 13:16

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-22 13:26

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];
查看更多
登录 后发表回答