我怎样才能得到最后插入的事件的ID

how can I get the id of the last inserted event

首先,我在 Xamarin.Android

上使用 SQLite-Net-Pcl

我的问题是,我需要创建一个递归的事件, 我需要连接所有这些递归事件(以防有人编辑或删除它们)。 我需要使用原始事件的 ID 将其连接到其他事件

-> creates new recursive event  
-> gets the id of inserted event  
-> uses that id to connect all recursive events together on a "NumDocConnection"

我的Id是自增的,而且是主键

那么,我怎样才能得到最后插入事件的id呢?或者这是处理这种情况的好方法?

how can I get the id of the last inserted event?

基于int的primary/auto-inc值是通过您插入的对象获得的(对象实例在Insert期间用新值更新)。

var newRecord = new Record { SomeString = "Whosebug" };
var numofRecordsInserted = conn.Insert(newRecord);
Console.WriteLine($"Newly inserted id = {newRecord.Key}");

is this even a good way do to deal with the situation

这取决于您的应用程序。 pri-key/auto-inc 字段会阻止重复使用生成的 ID,直到重置数据库(通过 deletion/re-creation 或通过 sqlite_sequence table 重置)。

你是否应该将它用作 "foreign" 键,这里有很多 opinions,有些是,有些不是......就个人而言,如果你从不在你的应用程序外部使用它(即它是不是 transmitted/sync 通过远程 API) 它作为外键工作 well/fast 如果你了解 SQLite 如何创建它以及它何时 can/could 被重置,请参阅链接文档.

If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.

回复:https://sqlite.org/autoinc.html