1

I want to open database as READONLY:

exit = sqlite3_open_v2( "database.db?immutable=1" , &db_ , SQLITE_OPEN_READONLY | SQLITE_OPEN_URI , nullptr );

but I am getting error: SQLITE_CANTOPEN, any idea why?

If I open it like this, open is successfull:

exit = sqlite3_open( "database.db" , &db_ );
5
  • C and C++ are different languages. You should probably tag the appropriate language for what you are doing. Commented Feb 6, 2020 at 17:07
  • I'm confused why you are trying to combine using the SQLITE_OPEN_READONLY flag with the SQLITE_OPEN_URI mode. I suggest trying just the READONLY flag and passing the normal database name as the first argument. (I think you need to precede the db name with file: anyway in URI mode). Alternatively, use only the URI flag, and then use file:database.db?mode=ro&immutable=1. Commented Feb 6, 2020 at 17:09
  • @BoBTFish looks like SQLITE_OPEN_URI enables URI filenames but non-uris still work: sqlite.org/c3ref/open.html "If URI filename interpretation is enabled, and the filename argument begins with "file:", then the filename is interpreted as a URI" Commented Feb 6, 2020 at 17:13
  • 1
    @AlanBirtles database.db?immutable=1 is not a non-URI, so it would need the file: prefix. Commented Feb 6, 2020 at 17:14
  • @BoBTFish yep, didn't spot that Commented Feb 6, 2020 at 17:14

1 Answer 1

1

When using the SQLITE_OPEN_URI "the filename can be interpreted as a URI". However, for it to be treated as a URI, it must be prefixed with file:, else it will be treated as a normal filename.

So you are trying to open a file named database.db?immutable=1, which obviously doesn't exist. Note that with SQLITE_OPEN_READONLY "if the database does not already exist, an error is returned". Therefore the simplest fix is simply to make the string be file:database.db?immutable=1.

However I suggest you stick to only one method of providing special options; use the SQLITE_OPEN_URI flag, and put all the other options into the URI, like so: file:database.db?mode=ro&immutable=1.

(I realise that not all combinations of options are possible using only a single method, but in your case it is, so I say stick to one and then you don't need to worry about the way different methods interact).

SQLite has extensive documentation, and you can read all about opening databases here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.