Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open SQLite connection in WAL mode

Tags:

c#

sqlite

wal

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)
like image 550
Nicolas Raoul Avatar asked Apr 08 '13 01:04

Nicolas Raoul


People also ask

What is WAL format?

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 are DB WAL files?

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.

What is journal mode in SQLite?

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.


2 Answers

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();
like image 86
Turbot Avatar answered Sep 19 '22 23:09

Turbot


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;")
like image 21
Nicolas Raoul Avatar answered Sep 18 '22 23:09

Nicolas Raoul