I have created an in memory database using the statement:
rc = sqlite3_open(":memory:", &db);
I need to access this database in other thread (for reading data).
Is there any way to access this in memory database in other thread?
The documentation says:
Every
:memory:database is distinct from every other. So, opening two
database connections each with the filename:memory:will create two
independent in-memory databases.
If I create multiple in memory database, how can I access these databases from other thread?
The
dbvariable in your code becomes the pointer to the SQLite db object created:Ref.: http://www.sqlite.org/capi3ref.html#sqlite3_open
This is the pointer you will use in your different threads to execute statements. For example, you prepare statements with the following function to which you’ll pass your
dbpointer:Ref.: http://www.sqlite.org/c3ref/prepare.html
EDIT:
I don’t know if you can change the name of the memory database that you open with
sqlite3_open(":memory:", ...), but it does have a name. Also you can attach additional in-memory databases.There is a good chance that the initially opened in-memory database name is
main. You can verify that by running the statementpragma database_list.You can attach more in-memory database by running statements such as
ATTACH DATABASE ':memory:' AS db2;.Here is a quick demo of the above using the sqlite3 client: