ruben.moor
ruben.moor

Reputation: 1965

How to open a Sqlite3 database in read-only mode with persistent?

I currently use createSqlitePool from persistent-sqlite:

import Database.Persist.Sqlite

createSqlitePool "mydb.db" 10

According to Sqlite3 documentation I can pass additional parameters in connection strings, e.g.

"Data Source=c:\mydb.db;Version=3;Read Only=True;"

Cf. https://www.connectionstrings.com/sqlite-net-provider/read-only-connection/

Apparently, it's not possible to simply pass this connection string to createSqlitePool. Is there a way to tell persistent-sqlite to open my database read-only?

Upvotes: 3

Views: 8283

Answers (2)

ruben.moor
ruben.moor

Reputation: 1965

Persistent now supports Sqlite3-URI-syntax in connection strings, as documented here:

https://github.com/yesodweb/persistent/wiki/Database-Configuration

Thus, for read-only something like this works:

URI filename style, relative path, read-only:

file:sqlite3.db?mode=ro

Upvotes: 4

Shaun the Sheep
Shaun the Sheep

Reputation: 22762

I think that's .NET specific information you're linking to, so it probably doesn't apply here.

If you look at the code, persistent just calls the SQLite C function sqlite3_open which only takes the filename of the database.

It looks like it would need to use sqlite3_open_v2 which take additional flags, if you wanted to make the connection read-only.

You can probably achieve the same thing using file permissions though, since a user won't be able to write the SQLite DB file if they don't have permission to do so.

Upvotes: 3

Related Questions