4

我正在尝试在 Windows 上使用 SQLAlchemy 和 Python 2.7.3 使用内存 SQLite 数据库。我可以连接到引擎,但是当我尝试执行第二条语句时,我收到以下错误:

>>>    engine=create_engine('sqlite:///memory:',echo=True)
>>>    engine.execute("select 1").scalar()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2445, in execute
connection = self.contextual_connect(close_with_result=True)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2489, in contextual_connect
self.pool.connect(),
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 236, in connect
return _ConnectionFairy(self).checkout()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 401, in __init__
rec = self._connection_record = pool._do_get()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 822, in _do_get
return self._create_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 189, in _create_connection
return _ConnectionRecord(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 282, in __init__
self.connection = self.__connect()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 344, in __connect
connection = self.__pool._creator()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 80, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 281, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file
None None
4

2 回答 2

17

文件名应该是:memory:,而不是memory:。(请参阅内存数据库的文档)。相关的SQLAlchemy 文档提到这是默认路径,因此您应该使用:

engine=create_engine('sqlite://',echo=True)

您收到的错误可能是因为memory:它不是 Windows 上的有效文件名。

于 2012-10-22T19:47:52.487 回答
0

以下对我有用:

from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool

engine = create_engine("sqlite://", poolclass=StaticPool)
于 2021-10-17T03:24:18.340 回答