:memory:当使用特殊URI打开数据库时,SQLite支持数据库的"共享缓存" (根据sqlite.org):
[T]他可以通过两个或多个数据库连接打开相同的内存数据库,如下所示:
rc = sqlite3_open("file::memory:?cache=shared",&db);
我可以通过利用这个在Python 3.4 URI参数为sqlite3.connect():
sqlite3.connect('file::memory:?cache=shared', uri=True)
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法为SQLAlchemy工作:
engine = sqlalchemy.create_engine('sqlite:///:memory:?cache=shared')
engine.connect()
...
TypeError: 'cache' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)
有没有办法让SQLAlchemy使用共享缓存?
编辑:
在Python 3.4上,我可以使用creator参数create_engine来解决问题,但问题仍然存在于其他Python版本中:
creator = lambda: sqlite3.connect('file::memory:?cache=shared', uri=True)
engine = sqlalchemy.create_engine('sqlite://', creator=creator)
engine.connect()
Run Code Online (Sandbox Code Playgroud) 是否可以在一个进程中访问数据库,在另一个进程中创建?我试过了:
空闲#1
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("create table test(testcolumn)")
c.execute("insert into test values('helloooo')")
conn.commit()
conn.close()
Run Code Online (Sandbox Code Playgroud)
IDLE#2
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("select * from test")
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
q = c.execute("select * from test")
sqlite3.OperationalError: no such table: test
Run Code Online (Sandbox Code Playgroud)