SQLite Forum

sqlite3_deserialize returning 'unable to open database file'
Login

sqlite3_deserialize returning 'unable to open database file'

(1) By fmzakari on 2023-10-16 21:58:19 [source]

I am using sqlite3_deserialize to open an embedded database in a file. The function itself returns successfully, but subsequent uses of the function (i.e. sqlite3_get_table) fail with 'unable to open database file'.

I'm a bit at a loss why it's giving this error if the deserialization was a success.

Roughly the code looks like: ``` sqlite3 *db;
int rc = sqlite3_open(":memory:", &db);

if (rc != SQLITE_OK) {
    printf("Cannot open database: %s\n", sqlite3_errmsg(db));
    exit(1);
}

rc = sqlite3_deserialize(db, "main", data, size, size,
                            SQLITE_DESERIALIZE_READONLY | SQLITE_DESERIALIZE_RESIZEABLE);
if (rc != SQLITE_OK) {
    printf("Cannot deserialize database: %s\n", sqlite3_errmsg(db));
    exit(1);
}

char * sql = "SELECT COUNT(*) FROM ELF_SYMBOLS";
int rows, columns;
char **result;

rc = sqlite3_get_table(db, sql, &result, &rows, &columns, NULL);
if (rc != SQLITE_OK) {
    printf("Failed to execute query[%d]: %s\n", rc, sqlite3_errmsg(db));
    exit(1);
}
printf("Number of rows in table: %s\n", result[columns]);

```

Appreciate any advice to debug.

(2) By fmzakari on 2023-10-16 22:54:05 in reply to 1 [link] [source]

Not sure if it was clear but all the prior calls, including deserialize, succeed but subsequent calls after deserialize to access the database fail with the error message.

(3) By fmzakari on 2023-10-17 01:15:30 in reply to 1 [link] [source]

I think I have figured it out -- has to do wit the fact that it's a WAL type database.

Wow that is incredibly tricky and very little info aside from me setting gdb breakpoints to have come to that conclusion.

I wonder if that should be an explicit check for deserialize with a log statement to help future users?

(4) By Stephan Beal (stephan) on 2023-10-17 01:31:23 in reply to 3 [link] [source]

I wonder if that should be an explicit check for deserialize with a log statement to help future users?

Here's a workaround: before passing the memory to deserialize(), overwrite byte offsets 18 and 19 with the value 1.

i've pinged Richard to ask whether we should do that automatically for deserialize (as we do in some of the JavaScript/WASM parts), but that may have negative side effects i'm not aware of.