【问题标题】:How to open SQLite connection in WAL mode如何在 WAL 模式下打开 SQLite 连接
【发布时间】:2013-03-30 00:05:38
【问题描述】:

在C#中,如何打开一个SQLite连接in WAL mode

这是我在正常模式下的打开方式:

SQLiteConnection connection = new SQLiteConnection("Data Source=" + file);
connection.Open();
// (Perform my query)

【问题讨论】:

    标签: c# sqlite wal


    【解决方案1】:

    在 SQLiteConnection 连接字符串中指定工厂方法怎么样?

    例如

    public static class Connection
    {
        public abstract SQLiteConnection NewConnection(String file);
    }
    
    public class NormalConnection : Connection 
    {
      public override SQLiteConnection NewConnection(String file)
      {
         return new SQLiteConnection("Data Source=" + file);
      }
    }
    
    public class WALConnection : Connection 
    {
      public override SQLiteConnection NewConnection(String file)
      {
        return new SQLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;"
      }
    }
    

    代码没有经过测试,但我希望你能明白,所以当你使用它时,你可以这样做。

       SQLiteConnection conWal = new WALConnection(file);
        conWAL.Open();
    
        SQLiteConnection conNormal = new NormalConnection(file);
        conNormal.Open();
    

    【讨论】:

    • +1 您代码的最后一行是我正在寻找的解决方案,非常感谢!工厂方法可能很有趣,即使我不需要它。
    • 鉴于 SQLite 连接字符串中允许的参数数量,您的方法是一个有趣的组合学案例研究:)
    • 这个解决方案对我不起作用。我可以将数据库放入 journal_mode=WAL 的唯一方法是在建立连接后发出单独的命令。
    【解决方案2】:

    下面这行是我一直在寻找的,非常感谢 Turbot 的回答:

    new SQLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;")
    

    【讨论】:

      【解决方案3】:

      WAL模式的持久化

      “与其他日志模式不同,PRAGMA journal_mode=WAL 是持久的。如果进程设置 WAL 模式,然后关闭并重新打开数据库,数据库将返回 WAL 模式。”

      http://www.sqlite.org/wal.html

      如果我理解正确,这意味着您可以为数据库设置一次 WAL 模式,无需在每个连接上都设置它。

      您可以使用 SQLite 的命令行 shell 来执行此操作: http://www.sqlite.org/sqlite.html

      【讨论】:

        【解决方案4】:

        这是我不太完美的解决方案:

        SQLiteConnection connection = new SQLiteConnection("Data Source=" + file);
        connection.Open();
        using (var command = new SQLiteCommand(sqliteConnection))
        {
            command.CommandText = "PRAGMA journal_mode=WAL";
            command.ExecuteNonQuery();
        }
        // (Perform my query)
        

        如果你知道一些不那么冗长的东西,我会很高兴听到它!

        【讨论】:

        • 我相信这是唯一正确的答案。上述在连接字符串中设置 PRAGMA 的答案对我不起作用。
        猜你喜欢
        • 1970-01-01
        • 2012-08-13
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 2016-08-26
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        相关资源
        最近更新 更多