多个 SQLite 连接到 :memory: 中的数据库
Multiple SQLite connections to a database in :memory:
是否可以从不同的线程访问内存中的 SQLite 数据库?
在下面的示例代码中我在内存中创建了一个SQLite数据库并创建了一个table。当我现在转到不同的执行上下文时,我认为当我转到不同的线程时必须这样做,创建的 table 不再存在。如果我打开一个基于文件的 SQLite 数据库,table 就会在那里。
我可以为内存数据库实现相同的行为吗?
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.
工作中!!
cacheDB = SqliteDatabase('file:cachedb?mode=memory&cache=shared')
Link
是否可以从不同的线程访问内存中的 SQLite 数据库?
在下面的示例代码中我在内存中创建了一个SQLite数据库并创建了一个table。当我现在转到不同的执行上下文时,我认为当我转到不同的线程时必须这样做,创建的 table 不再存在。如果我打开一个基于文件的 SQLite 数据库,table 就会在那里。
我可以为内存数据库实现相同的行为吗?
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.
工作中!!
cacheDB = SqliteDatabase('file:cachedb?mode=memory&cache=shared')