OperationalError“无法打开数据库文件”使用SQLAlchemy和SQLite3处理查询结果

时间:2010-05-10 15:50:13

标签: python sqlite sqlalchemy

我遇到了这个小问题,我希望这只是一个愚蠢的用户错误。对于SQLite数据库的查询,它看起来像某种大小限制。我设法使用内存数据库和下面显示的简单脚本重现该问题。我可以通过减少数据库中的记录数量来使其工作;或通过减少每条记录的大小;或者通过删除order_by()调用。我在Cygwin环境中使用Python 2.5.5和SQLAlchemy 0.6.0。

谢谢!

#!/usr/bin/python
from sqlalchemy.orm import sessionmaker
import sqlalchemy
import sqlalchemy.orm

class Person(object):
    def __init__(self, name): self.name = name

engine = sqlalchemy.create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
metadata = sqlalchemy.schema.MetaData(bind=engine)
person_table = sqlalchemy.Table('person', metadata,
        sqlalchemy.Column('id', sqlalchemy.types.Integer, primary_key=True),
        sqlalchemy.Column('name', sqlalchemy.types.String))
metadata.create_all(engine)
sqlalchemy.orm.mapper(Person, person_table)

session = Session()
session.add_all([Person("012345678901234567890123456789012")
                 for i in range(5000)])
session.commit()

persons = session.query(Person).order_by(Person.name).all()
print "count =", len(persons)

session.close()

对查询结果的all()调用失败,出现OperationalError异常:

Traceback (most recent call last):
  File "./stress.py", line 27, in <module>
    persons = session.query(Person).order_by(Person.name).all()
  File "/usr/lib/python2.5/site-packages/sqlalchemy/orm/query.py", line 1343, in all
    return list(self)
  File "/usr/lib/python2.5/site-packages/sqlalchemy/orm/query.py", line 1451, in __iter__
    return self._execute_and_instances(context)
  File "/usr/lib/python2.5/site-packages/sqlalchemy/orm/query.py", line 1456, in _execute_and_instances
    mapper=self._mapper_zero_or_none())
  File "/usr/lib/python2.5/site-packages/sqlalchemy/orm/session.py", line 737, in execute
    clause, params or {})
  File "/usr/lib/python2.5/site-packages/sqlalchemy/engine/base.py", line 1109, in execute
    return Connection.executors[c](self, object, multiparams, params)
  File "/usr/lib/python2.5/site-packages/sqlalchemy/engine/base.py", line 1186, in _execute_clauseelement
    return self.__execute_context(context)
  File "/usr/lib/python2.5/site-packages/sqlalchemy/engine/base.py", line 1215, in __execute_context
    context.parameters[0], context=context)
  File "/usr/lib/python2.5/site-packages/sqlalchemy/engine/base.py", line 1284, in _cursor_execute
    self._handle_dbapi_exception(e, statement, parameters, cursor, context)
  File "/usr/lib/python2.5/site-packages/sqlalchemy/engine/base.py", line 1282, in _cursor_execute
    self.dialect.do_execute(cursor, statement, parameters, context=context)
  File "/usr/lib/python2.5/site-packages/sqlalchemy/engine/default.py", line 277, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file u'SELECT person.id AS person_id, person.name AS person_name \nFROM person ORDER BY person.name' ()

3 个答案:

答案 0 :(得分:2)

问题是您的python脚本无法找到 SQLite 文件。设置文件的正确路径,问题就解决了。

答案 1 :(得分:2)

我能够运行你的代码,所以它确实有效。但是,我发现SQLAlchemy的Core更适合于性能依赖的情况,例如,使用ORM时会有一些开销。我还专门调用SQLAlchemy类来避免加载整个SQLAlchemy库。

以下内容应该更加精简,如果您拥有它们,希望避免任何内存问题:

from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, select

engine = create_engine('sqlite:///:memory:', echo=False)
metadata = MetaData()
person_table = Table('person', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String))
metadata.create_all(engine)

session = engine.connect()

for i in range(5000):
    session.execute(person_table.insert().values(name='012345678901234567890123456789012'))
persons = select([person_table.c.name])
persons = persons.order_by(person_table.c.name.asc())
result = session.execute(persons)
session.close()

count = 0
for i in result: count += 1
print "count =", count

答案 2 :(得分:1)

更改为:

persons = session.query(Person).order_by(Person.name).count()
print "count =", persons

工作?如果没有,我怀疑常量字段上的order_by正在吹一些内部表。你也可以找到

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

对调试很有用。它看起来不像5000个简单的行应该引起麻烦。当然,在我的Linux 2.6.32上使用SQLAlchemy 0.6.0它的工作没有问题:内存:或真正的文件...抱歉。