Predict next auto-inserted row id (SQLite)

前端 未结 11 1358
予麋鹿
予麋鹿 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:42

    Either scrapping or committing a series of database operations all at once is exactly what transactions are for. Query BEGIN; before the user starts fiddling and COMMIT; once he/she's done. You're guaranteed that either all the changes are applied (if you commit) or everything is scrapped (if you query ROLLBACK;, if the program crashes, power goes out, etc). Once you read from the db, you're also guaranteed that the data is good until the end of the transaction, so you can grab MAX(id) or whatever you want without worrying about race conditions.

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

提交回复
热议问题