how to open a SQLite3 database as read only
(1) By colinb8 on 2021-06-06 19:44:03 [source]
I'd like to be able to open a database as read only. I've tried quite a bit of WWW searching. There is this:
https://sqlite.org/c3ref/open.html
... The sqlite3_open_v2() interface works like sqlite3_open() except that it accepts two additional parameters for additional control over the new database connection. The flags parameter to sqlite3_open_v2() must include, at a minimum, one of the following three flag combinations: SQLITE_OPEN_READONLY The database is opened in read-only mode. If the database does not already exist, an error is returned. ...
https://sqlite.org/c3ref/c_open_autoproxy.html
... Flags For File Open Operations
#define SQLITE_OPEN_READONLY 0x00000001 ...
So it seems to be possible using a 'C' interface.
Running sqlite3.exe from the command line in Windows 10, then at the 'sqlite>' prompt entering '.help' shows the available '.' commands, including:
.open ?OPTIONS? ?FILE? Close existing database and reopen FILE
suggesting it might be possible in 'sqlite>' but when I try
.open 1 /pathto/mytest.db
where '/pathto/mytest.db' is a database I previously created using 'sqlite>' I'm getting this error message:
extra argument: "/pathto/mytest.db"
Are any '?OPTIONS?' actually possible?
Thanks
(2) By Larry Brasfield (larrybr) on 2021-06-06 21:26:40 in reply to 1 [link] [source]
Take a look at the shell's response to
.help open
, where you will see what options are possible.
Note also that values defined for the C interface do not translate unchanged to values which are meaningful for the shell's interface.
(3) By colinb8 on 2021-06-07 04:24:59 in reply to 2 [link] [source]
Thank you. It hadn't occurred to me that '.help' could be used for individual commands.
That tells me what I needed, and there is a '--readonly' option which I've just tried and it works. Unfortunately what I'm trying to do is open the Chrome browser History file while simultaneously running a Chrome browser: I know I can find the History from within Chrome - I do have reasons for wanting the History simultaneously from outside Chrome! I had hoped using the SQLite command line interface opening History as read only would work, but it seems '--readonly' is insufficient to overcome a Chrome lock on History because when I then try '.tables' I get a message 'Error: database is locked'. There must be a way to do what I want because https://www.nirsoft.net/web_browser_tools.html can read the History file while a Chrome browser is running. I've just noticed that that tool also has a command line interface option, and I've tried it, and it does what I want.
So, again thank you for your help, and this query is now most satisfactorily answered, so is now closed!
(4) By Clemens Ladisch (cladisch) on 2021-06-07 11:28:40 in reply to 3 [link] [source]
Chrome opens the database exclusive mode.
I guess that tool just copies the raw files (which will result in corrupted data if writes have not yet been flushed).