Question

I want to create a shared, in-memory database with SQLite by passing it URI like file:memdb1?mode=memory&cache=shared.

The following code looks simple enough to get the job done:

#include <iostream>
#include <sqlite3.h>

void main()
{
  sqlite3 * db;
  int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI;
  int returnCode = sqlite3_open_v2("file:memdb1?mode=memory&cache=shared", &db, flags, NULL);

  if (returnCode == SQLITE_OK)
    std::cout << "Success!" << std::endl;
  else
    std::cout << "Error: " << sqlite3_errmsg(db) << std::endl;

  if (db) sqlite3_close(db);
}

Then I compile it like this:

$ g++ sample.cpp -lsqlite3

And this is what I get:

$ ./a.out
Error: no such access mode: memory

I have tried many different configurations for flags (some including SQLITE_OPEN_MEMORY) and I have not been able to get this simple program to run. I've tried it on both Windows (with a custom build of SQLite 3.7.15) and on Ubuntu (using the default apt-get package, version 3.7.9) with no success.

Has anyone encountered this and discovered a solution?

Was it helpful?

Solution

It turns out I was using the wrong version of SQLite. Shared caches are not supported in SQLite until version 3.7.13 (see changelog).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top