Predict next auto-inserted row id (SQLite)

前端 未结 11 1399
予麋鹿
予麋鹿 2020-12-03 17:23

I\'m trying to find if there is a reliable way (using SQLite) to find the ID of the next row to be inserted, before it gets inserted. I need to use the id for anoth

11条回答
  •  我在风中等你
    2020-12-03 17:55

    select max(id) from particular_table is unreliable for the reason below..
    

    http://www.sqlite.org/autoinc.html

    "The normal ROWID selection algorithm described above will generate monotonically increasing unique ROWIDs as long as you never use the maximum ROWID value and you never delete the entry in the table with the largest ROWID. If you ever delete rows or if you ever create a row with the maximum possible ROWID, then ROWIDs from previously deleted rows might be reused when creating new rows and newly created ROWIDs might not be in strictly ascending order."

提交回复
热议问题