last_insert_rowid()
来自最后一个插入 ANY 表的行 ID 中的结果。绝对不是其他答案中提到的线程安全。
如果您绝对需要确保返回正确的行 ID,无论线程、异步等如何(例如,如果您打算将 rowid 用作另一个表中的外键),这里有一种方法:
- 在所需的表中插入一个文本列(这将包含一个 GUID)
- 手动生成 GUID(使用您的语言中可用的任何库)并将其保存在内存中
- 将数据与刚刚生成的 GUID 一起插入表中
- 通过检索(假定的)rowid
last_insert_rowid()
- 使用此 rowid 从表中检索行(或仅 GUID)
- 将检索到的行中的 GUID 与内存中的 GUID 进行比较
- if they are the same, happy days, you have the correct rowid
- if they are different, you need to query the table for a match to the GUID you have in memory for the correct rowid
Obviously this solution has considerable performance drawbacks and you need to make a judgement call on whether the risks of mismatched data outweigh the performance toll. In my case, I needed to be certain of the integrity of my data above all else so it was fine for performance to take the hit. (Just how big a hit, I can't say.)
You might get the urge to skip the 4th and 5th steps and get the rowid using your GUID; you might also get the urge to just use the GUID as the primary identifier and foreign key. After all these would make the code simpler and mean less columns in your table... RESIST THAT URGE. Integers as primary identifiers are easily indexable which makes them faster/more efficient in WHERE clauses and JOINS; a GUID or string just doesn't index as well as an integer.