How to open a SQLite connection in WAL mode

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

Here's how I open in normal mode:

SQLiteConnection connection = new SQLiteConnection("Data Source=" + file); connection.Open(); // (Perform my query) 
+6
source share
4 answers

What about the factory approach for specifying SQLiteConnection in the connection string?

eg,

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

The code is untested, but I hope you can get this idea, so when you use it, you can do it.

  SQLLiteConnection conWal = new WALConnection(file); conWAL.Open(); SQLLiteConnection conNormal = new NormalConnection(file); conNormal.Open(); 
+10
source

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

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

Here is my less 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 anything less details, I would be happy to hear about it!

+1
source

Saving WAL mode

"Unlike other logging modes, PRAGMA journal_mode = WAL is constant. If the process sets to WAL mode, then closes and opens the database, the database will return to WAL mode."

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

If I understand correctly, this means that you can set the WAL mode for the database once, there is no need to set it for each connection.

You can do this using the command line for SQLite: http://www.sqlite.org/sqlite.html

+1
source

All Articles