9

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

2 Answers 2

25

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.

4
  • this aint working...n i am just new on sqlalchemy so i wanted to know that whether the database sqlite has to be installed separately or does it comes inbuilt with the sqlalchemy package.If i have to install sqlite separately then where should i instaal it .You know give me info on the configuration. Oct 24, 2012 at 8:48
  • @user1742141 sqlite comes with every Python installation, you shouldn't need to deal with this.
    – millimoose
    Oct 24, 2012 at 14:02
  • @user1742141 See if you can just import sqlite3 in a console?
    – millimoose
    Oct 24, 2012 at 14:11
  • @user1742141 I just tried your code with the fix from my answer on a clean-ish Python install under Windows and it works. Are you sure you're not using three slashes (///) instead of two (//)?
    – millimoose
    Oct 24, 2012 at 14:31
2

The following worked for me:

from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool

engine = create_engine("sqlite://", poolclass=StaticPool)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.