Ran*_*ian 4 python sqlite peewee
是否可以从不同线程访问内存中的 SQLite 数据库?
在以下示例代码中,我在内存中创建了一个 SQLite 数据库并创建了一个表。当我现在转到不同的执行上下文时,我认为当我转到不同的线程时必须这样做,创建的表不再存在。如果我打开一个基于文件的 SQLite 数据库,表就会在那里。
我可以为内存数据库实现相同的行为吗?
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.
Run Code Online (Sandbox Code Playgroud)
cacheDB = SqliteDatabase('file:cachedb?mode=memory&cache=shared')
| 归档时间: |
|
| 查看次数: |
2749 次 |
| 最近记录: |