Is it possible to access an in-memory SQLite database from different threads?
In the following sample code I create a SQLite database in memory and create a table. When I now go to a different execution context, which I think have to do when I go to a different thread, the created table isn't there anymore. If I would open a file based SQLite database, the table would be there.
Can I achieve the same behavior for an in-memory database?
from peewee import *
db = SqliteDatabase(':memory:')
class BaseModel(Model):
class Meta:
database = db
class Names(BaseModel):
name = CharField(unique=True)
print(Names.table_exists()) # this returns False
Names.create_table()
print(Names.table_exists()) # this returns True
print id(db.get_conn()) # Our main thread's connection.
with db.execution_context():
print(Names.table_exists()) # going to another context, this returns False if we are in :memory: and True if we work on a file *.db
print id(db.get_conn()) # A separate connection.
print id(db.get_conn()) # Back to the original connection.
file::memory:?cache=shared
to share an in-memory database. Requires sqlite 3.7.13 or newer (inspectsqlite3.sqlite_version
orsqlite3.sqlite_version_info
). – Ragwortfile::memory:?cache=shared
unfortunately doesn't work:from peewee import * db = SqliteDatabase('file::memory:?cache=shared') db.connect()
gives me a file not found error. My SQLite version is 3.11.0 – Groundlingdb = sqlite3.connect("file::memory:?cache=shared", uri=True)
complains that uri is no valid keyword and when I omit it, a "file could not be opened" exception is thrown. It seems that I shouldn't use the in-memory database if I want to do threaded access. – Groundlingsqlite3
library version that I may not be aware of.uri=True
is a Python 3.4 addition, but myfile::memory:?cache=shared
answer only uses Python 2.7. – Ragwort