Skip to content

Add hook for sending pragmas before opening connection #29186

Description

@rgroenewoudt

SQLite and SQLCipher have pragma's that need to be set before "normal" queries are executed.

However when the password is set in the connection string, the "normal" query SELECT COUNT(*) FROM sqlite_master; (SqliteConnectionInternal.cs:120) is executed, preventing execution of these special pragma.

var builder = new SqliteConnectionStringBuilder
{
	DataSource = Path.GetTempFileName(),
	Cache = SqliteCacheMode.Private,
	Mode = SqliteOpenMode.ReadWriteCreate,
	Pooling = false,

	Password = Guid.NewGuid().ToString()
};

for(int i = 0; i < 2; i++)
{
	using var conn = new SqliteConnection(builder.ConnectionString);
	conn.Open(); // Fails 2nd time with Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 26: 'file is not a database'.'

	using (var command = conn.CreateCommand())
	{
		command.CommandText = "PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA256;";
		command.ExecuteNonQuery();
	}

	using (var command = conn.CreateCommand())
	{
		command.CommandText = "PRAGMA journal_mode = WAL";
		command.ExecuteNonQuery();
	}
}

ConsoleApp11.zip

A few pragma's need to be set before querying sqlite_master, for example:

  • journal_mode
  • kdf_iter
  • cipher_kdf_algorithm
  • cipher_page_size

It would be nicer if I can somehow define PRAGMAs in a SQLiteConnection before opening the connection. This would also work better with connection pooling because I don't have to manually execute the PRAGMA every time when opening a connection.

Include version information

Microsoft.Data.Sqlite version: 6.0.9
SQLitePCLRaw: 2.1.2
Target framework: .NET 6
Operating system: Windows & Android

Activity

  1. roji commented on Sep 23, 2022

    @roji
    Member

    Could be a good candidate for a Sqlite DbDataSource feature (i.e. pragmas to set on all created connections at the outset)

  2. changed the title [-]Setting password in combination with other pragma fails[/-] [+]Add hook for sending pragmas before opening connection[/+] on Sep 27, 2022
  3. ajcvickers commented on Sep 27, 2022

    @ajcvickers
    Contributor

    @rgroenewoudt For now, a workaround is to not set the password in the connection string. In the future we may add a hook that would allow pragams to be set before the connection is opened.

  4. added this to the Backlog milestone on Sep 27, 2022
  5. rgroenewoudt commented on Sep 28, 2022

    @rgroenewoudt
    Author

    @ajcvickers: Is there some way to detect if an pooled SQLiteConnection is a new connection or an existing one from the pool?

    I would need to only execute all pragma's on the new connections. Otherwise I would run into the same performance issue as #28774 as resending pragma key is very slow.

  6. chylex commented on Jul 19, 2023

    @chylex

    I worked around this by turning off pooling, and using a custom pool that opens the connections and runs initial pragma commands. Still, an official solution would be better.

  7. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions