【问题标题】:Microsoft.Data.SQLite (.net core) get atomic last_insert_rowidMicrosoft.Data.SQLite(.net 核心)获取原子 last_insert_rowid
【发布时间】:2016-10-05 01:51:35
【问题描述】:

我想跨线程共享 SQLite 连接。

如果我在 SQLite shell 中键入以下内容:

sqlite>开始;插入个人值('Tweety','Bird',80);选择 last_insert_rowid();提交;

shell 打印新的 rowid。

在使用 Microsoft.Data.SQLite 的 dotnet 核心控制台应用程序中

            SqliteCommand command = new SqliteCommand("begin; insert into Person values('Tweety','Bird',81); select last_insert_rowid(); commit;", connection);

Console.WriteLine(command.ExecuteScalar());

我希望控制台会打印新的 rowid,但 ExecuteScalar() 返回 null。我已确认已插入新行。

  1. 是否可以通过一个组合命令获取新的 rowid?
  2. 如果我通过多个线程共享连接,这是一个原子事务吗?

【问题讨论】:

  • 您可以在单独的命令中执行“select last_insert_rowid()”(连接应保持打开状态)。
  • 但是共享连接的另一个线程可以同时插入一个新行吗?
  • 通常的做法是为每个线程使用单独的数据库连接;由于连接池,使用效率保持良好。在一个连接会话期间(在打开和关闭之间)“last_insert_rowid()”返回此会话中执行的最后一次插入的 id。
  • 谢谢。我会引用你的评论作为答案。

标签: sqlite .net-core


【解决方案1】:

我将引用 Vitaliy Fedorchenko 的评论:

通常的做法是为每个线程使用单独的数据库连接;由于连接池,使用效率保持良好。在一个连接会话期间 (在打开和关闭之间)“last_insert_rowid()”返回在此会话中执行的最后一次插入的 id。

【讨论】:

    猜你喜欢
    • 2015-05-18
    • 2017-12-17
    • 2023-02-05
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多