I'd like to open an sqlite3 database, and immediately check that it is not opened read-only. Since I know I'll need to write to it, I want to return an error immediately at start-up, and not have to wait until a later time when the program first tries to do a write.
I see there is a sqlite3 C API function sqlite3_db_readonly(). Is it possible to access this function from the Python API? Or is there some other way to check if a database is read-only from the Python API?
Apart from other possibilities mentioned, you can just try writing to it. For example, if the user_version of the database is 0 (which it will be unless you have changed it), issue pragma user_version=0;. This will have no effect if the database is writable, but will result in an error if the database is read-only.
no and yes.see the source code of the python standard library sqlite3.
rc = sqlite3_open_v2(database, &self->db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
(uri ? SQLITE_OPEN_URI : 0), NULL);
this means even if you didn't set the uri,it will open in read-only mode when the file in read-only mode.and search in the code.there are no invoke of sqlite3_db_readonly().so you cannot do it by library sqlite3.
but yes,there are other library,for exampleAPSW.the code
`static PyObject*
Connection_readonly(Connection *self, PyObject *name)
{
int res=-1;
res=sqlite3_db_readonly(self->db, PyBytes_AS_STRING(utf8name));
}`
then read the DOC,just call Connection.readonly(name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With