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

前端 未结 4 1122
日久生厌
日久生厌 2020-12-14 01:40

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 :

S         


        
4条回答
  •  天涯浪人
    2020-12-14 02:10

    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];
    

提交回复
热议问题