How to open SQLite connection in WAL mode

后端 未结 4 2120
青春惊慌失措
青春惊慌失措 2020-12-17 16:03

In C#, how to open an SQLite connection in WAL mode?

Here is how I open in normal mode:

SQLiteConnection connection = new SQLiteConnection(\"Data Sou         


        
相关标签:
4条回答
  • 2020-12-17 16:11

    The line below is what I was looking for, many thanks to Turbot whose answer includes it:

    new SQLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;")
    
    0 讨论(0)
  • 2020-12-17 16:26

    Here is my less-than-perfect solution:

    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)
    

    If you know something less verbose, I would be happy to hear about it!

    0 讨论(0)
  • 2020-12-17 16:28

    Persistence of WAL mode

    "Unlike the other journaling modes, PRAGMA journal_mode=WAL is persistent. If a process sets WAL mode, then closes and reopens the database, the database will come back in WAL mode."

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

    If I understand it correctly, this means that you can set WAL mode for a database once, there's no need to set it on every connection.

    You can do it with the command line shell for SQLite: http://www.sqlite.org/sqlite.html

    0 讨论(0)
  • 2020-12-17 16:29

    how about a factory approach to specify in the SQLiteConnection connetion string ?

    for e.g

    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;"
      }
    }
    

    The code is not tested, but I hope you can get the idea, so when you use it you can do like that.

       SQLiteConnection conWal = new WALConnection(file);
        conWAL.Open();
    
        SQLiteConnection conNormal = new NormalConnection(file);
        conNormal.Open();
    
    0 讨论(0)
提交回复
热议问题