- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个程序可以处理带线程的数据库(根据用户请求)。但是有一个问题:
虽然这不太可能,但还是有可能出现问题,例如,将用户插入数据库(他的详细信息)并且我想找出他的标识符列值(int
)使用 sqlite3_int64 sqlite3_last_insert_rowid(sqlite*);
函数。
尽管 API 通常是线程安全的,但正如我所研究的那样 - 如果另一个线程在我插入我的之后但之前将一行插入到同一个表中,则可能会出现问题 我调用了 sqlite3_last_insert_row_id()
。
If a separate thread performs a new INSERT on the same database connection while the sqlite3_last_insert_rowid() function is running and thus changes the last insert rowid, then the value returned by sqlite3_last_insert_rowid() is unpredictable and might not equal either the old or the new last insert rowid.
如何防止这种情况?
我可以互斥我所有的数据库访问,但在当前阶段这会很麻烦,这是唯一的方法吗?
我也可以做一个事务-sql 查询,但这会产生我想要的相同效果吗?
最佳答案
为了防止多线程相互干扰,应该使用事务,每个线程一个数据库连接。
如果你真的想在线程之间共享相同的连接,你需要一个互斥体。
关于c++ - 线程安全的 last_insert_rowid 与 SQLite3 API (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541436/
假设我有一个包含两个表的数据库 Persons和 PhoneNumbers ,其中PhoneNumbers表有一个指向 Persons 的外键.如果我想在单笔交易中插入一个有电话号码的人,我可以写一个
我想在插入语句后获取 last_insert_rowid()。 我想使用 SQLiteDatabase.execSQL() 但它说我不能在那里使用 select 语句。 如何使用 last_inser
我是 Android 新手。 我想在 TextView 中显示一个新 ID。所以,我只是想获取已存储在数据库中的最新 ID,并声明为 Integer 将我获取的值加 1,然后显示到 TextView。
我要执行的文件中包含多个SQL语句。 INSERT INTO reports (a,b,c) VALUES (1,2,3); INSERT INTO units (report_id, e, f, g
我有两张 table ,其中一张是另一张的助手。表“reviews”有一列是一个整数,引用表“films”中该评论的电影的id。有没有办法在“电影”表中插入新电影后,使用“评论”表中的 last_in
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我使用 node.js sqlite3 来操作数据。我使用这些代码将数据插入数据库并获取插入的 ID: db.run("INSERT INTO myTable (name) VALUES ('test
我对“last_insert_rowid()”有疑问。 在我的 DB-Helper-Class 中,我正在执行以下操作: public int getLastID() { final Stri
我有一个程序可以处理带线程的数据库(根据用户请求)。但是有一个问题: 虽然这不太可能,但还是有可能出现问题,例如,将用户插入数据库(他的详细信息)并且我想找出他的标识符列值(int)使用 sqlite
我是一名优秀的程序员,十分优秀!