gpt4 book ai didi

python - OperationalError "unable to open database file"使用 SQLAlchemy 和 SQLite3 处理查询结果

转载 作者:IT王子 更新时间:2023-10-29 06:26:08 27 4
gpt4 key购买 nike

我遇到了这个小问题,我希望它只是一个愚蠢的用户错误。它看起来像是对 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' ()

最佳答案

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

关于python - OperationalError "unable to open database file"使用 SQLAlchemy 和 SQLite3 处理查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2804106/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com