In C#, how to open an SQLite connection in WAL mode?
Here is how I open in normal mode:
SQLiteConnection connection = new SQLiteConnection("Data Source=" + file);
connection.Open();
// (Perform my query)
The write-ahead log or "wal" file is a roll-forward journal that records transactions that have been committed but not yet applied to the main database. Details on the format of the wal file are describe in the WAL format subsection of the main file format document.
What is a DB-WAL file? Temporary database file created by SQLite 3.7. 0 or later, a compact database program commonly embedded into mobile and desktop applications; saves write-ahead log (WAL) information for the database; automatically created and managed by the database.
A DB-JOURNAL file is a temporary database file created by SQLite database management systems during a transaction between an application and a database. It contains a rollback journal, which is a temporary database that stores the most recent state of the database.
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();
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;")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With