Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot connect to in-memory SQLite DB using SQLAlchemy with Python 2.7.3 on Windows

I am trying to use the in-memory SQLite database using SQLAlchemy with Python 2.7.3 on Windows. I can connect to the engine, but when I try to execute the second statement I am getting the following error:

>>>    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
like image 702
user1742141 Avatar asked Dec 04 '22 01:12

user1742141


1 Answers

The filename should be :memory:, not memory:. (See the docs for in-memory databases). The relevant SQLAlchemy docs mention that's the default path, so you should use:

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

The error you're getting is presumably because memory: isn't a valid filename on Windows.

like image 181
millimoose Avatar answered May 25 '23 23:05

millimoose