预测下一个自动插入的行ID(SQLite) [英] Predict next auto-inserted row id (SQLite)

查看:115
本文介绍了预测下一个自动插入的行ID(SQLite)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否有可靠的方法(使用 SQLite的)找到要插入的下一行的ID,插入之前。我需要使用id另一插入语句,但没有即刻插入并获得下一行的选项。

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 another insert statement, but don't have the option of instantly inserting and getting the next row.

时的预测下一个ID那样简单获取最后一个ID并添加一个?这是保证吗?

Is predicting the next id as simple as getting the last id and adding one? Is that a guarantee?

编辑:多一点推理...
我不能立即插入,因为插入可能最终被用户取消。用户将进行一些更改,SQL语句将被存储,从那里用户可以保存(一次插入所有行)或取消(不更改任何内容)。在程序崩溃的情况下,所需的功能是毫无得到改变。

A little more reasoning... I can't insert immediately because the insert may end up being canceled by the user. User will make some changes, SQL statements will be stored, and from there the user can either save (inserting all the rows at once), or cancel (not changing anything). In the case of a program crash, the desired functionality is that nothing gets changed.

推荐答案

无论是报废或实施了这一系列数据库操作一次就是交易。在用户开始fiddling之前查询 BEGIN; ,一旦他/她完成, COMMIT; 你保证,要么所有的更改应用(如果你犯)或一切报废(如果你查询 ROLLBACK; ,如果程序崩溃,停电等)。一旦你从数据库读取,你也保证了数据良好,直到事务结束,这样你就可以抢 MAX(ID)或任何你想不担心种族条件。

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

这篇关于预测下一个自动插入的行ID(SQLite)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆