Hello,
I connect to the same database file twice, first in readonly mode and
then in readwrite mode while having set sqlite3_enable_shared_cache(1).
Any attempt to change any data using the second database connection
results in an "attempt to write a readonly database" error.
The same code works fine with sqlite3_enable_shared_cache(0).
tested in sqlite version 3.11.0 and 3.10.2
example code:
------------------------
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
int main(int argc, char *argv[])
{
sqlite3* dbreadonly;
sqlite3* dbreadwrite1;
sqlite3* dbreadwrite2;
int rc;
const char *pzTail = NULL;
sqlite3_enable_shared_cache(1);// code works with
sqlite3_enable_shared_cache(0);
// create dummy table
rc = sqlite3_open_v2("test.db", &dbreadwrite1,
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL);
const char* sqlreadwrite1 = "CREATE TABLE TEST(ID INTEGER);";
sqlite3_stmt *stmtreadwrite1;
rc = sqlite3_prepare_v2(dbreadwrite1, sqlreadwrite1,
strlen(sqlreadwrite1),&stmtreadwrite1, &pzTail);
rc = sqlite3_step(stmtreadwrite1);
sqlite3_finalize(stmtreadwrite1);
sqlite3_close(dbreadwrite1);
// open readonly database
rc = sqlite3_open_v2("test.db", &dbreadonly, SQLITE_OPEN_READONLY,
NULL);
if( rc ) printf("Can't open readonly database: %s\n",
sqlite3_errmsg(dbreadonly));
// open readwrite database and try to drop table
rc = sqlite3_open_v2("test.db", &dbreadwrite2,
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL);
if( rc ) printf("Can't open database: %s\n",
sqlite3_errmsg(dbreadwrite2));
const char* sqlreadwrite2 = "DROP TABLE IF EXISTS TEST";
sqlite3_stmt *stmtreadwrite2;
rc = sqlite3_prepare_v2(dbreadwrite2, sqlreadwrite2,
strlen(sqlreadwrite2),&stmtreadwrite2, &pzTail);
rc = sqlite3_step(stmtreadwrite2); // ********** error occurs here
"attempt to write a readonly database"
if( rc != SQLITE_DONE) printf("Can't drop table: %s\n",
sqlite3_errmsg(dbreadwrite2));
sqlite3_finalize(stmtreadwrite2);
sqlite3_close(dbreadwrite2);
sqlite3_close(dbreadonly);
}
--------------------------
Regards,
Ludger Kr?mer