Bhargava Bhargava - 27 days ago 6
C Question

Is it possible to connect to an in-memory sqlite db in Read-Only mode?

I know that I can open multiple connections to an In-Memory sqlite database using

file:DB_NAME?mode=memory&cache=shared
in
sqlite3_open_v2()
.

I open 2 connections to an In-Memory database. One with the flags
SQLITE_OPEN_URI | SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE
and another with
SQLITE_OPEN_READONLY | SQLITE_OPEN_URI
.

The problem is that sqlite lets me modify the database even when the connection is Read-Only.

Is there any way to make the connection Read-Only? Should I write my own VFS to accomplish it?

CL. CL.
Answer

The SQLITE_OPEN_READONLY flag affects how the database accesses any files and handles transactions.

In shared-cache mode, multiple connections appear as a single connection in the interface to the file system. Therefore, they share the file access/transaction settings.

To prevent a connection from starting any write transactions, use PRAGMA query_only.