Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 961
Bhargava Srinarasi Avatar asked Nov 11 '16 11:11

Bhargava Srinarasi


People also ask

How do I connect to a memory in SQLite database?

The most common way to force an SQLite database to exist purely in memory is to open the database using the special filename ":memory:". In other words, instead of passing the name of a real disk file into one of the sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2() functions, pass in the string ":memory:".

Does SQLite supports in memory databases?

SQLite in-memory databases are databases stored entirely in memory, not on disk. Use the special data source filename :memory: to create an in-memory database. When the connection is closed, the database is deleted. When using :memory: , each connection creates its own database.

How many connections can SQLite handle?

The default limit is 1,024.

How do I access SQLite database from another computer?

SQLite isn't a network database, so it doesn't have any network connection ability built into it. You would need to either: Make the SQLite DB available on a network-accessible shared drive OR. Write/find an existing network server/web service for it.


1 Answers

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.

like image 58
CL. Avatar answered Sep 22 '22 00:09

CL.