Opening sqlite files in readonly directory

This applies to using sqlite3 in C/C++.

By “default” SQLite3 creates WAL and SHM files in the same directory as the database file, even when opening in “read only” mode. I found that you need to be running a relatively new (2018) version of sqlite to get this to work the way I want (not creating these additional files when I truly want a read-only mode), AND I need to open like this…

rc = sqlite3_open_v2("file:/location/of/file.sqlite?immutable=1", &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);

The deviations from the way I was opening sqlite files (previously assuming I have write access to the directory that sqlite file(s) reside in) was to use the URI syntax (“file:” …), appending the parameter string “?immutable=1”, and the option SQLITE_OPEN_URI. You would *think* that just passing SQLITE_OPEN_READONLY was enough!

After doing this, I can open sqlite files in C/C++ in a read only mode without sqlite attempting to create WAL and SHM files (causing an error and preventing the sqlite file from opening).

Leave a Comment