gpt4 book ai didi

python - 升级到 Peewee 2.6 后出现 sqlite3 InterfaceError

转载 作者:太空宇宙 更新时间:2023-11-03 17:33:00 25 4
gpt4 key购买 nike

从 v2.4.7 升级到 Peewee 2.6.3 后,迭代选择查询的结果时会弹出以下错误。

编辑:这似乎是与 create_or_get 方法的使用相关的错误。

 File "./script.py", line 137, in load_data
for name,country in rows:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 1957, in next
obj = self.iterate()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 1934, in iterate
row = self.cursor.fetchone()
sqlite3.InterfaceError: Cursor needed to be reset because of commit/rollback and can no longer be fetched from.

这是有问题的代码:

for i in range(1, nb_rows // CHUNK_SIZE + 2):                                                                                                                                                                                                                                                                                                     
rows = (Source.select(Source.name,Source.country)
.where(Source.status == 'new').paginate(i, CHUNK_SIZE).tuples())

for name,country in rows:
person, created = Person.create_or_get(name = name, new = True)

if country:
country, created = Countries.create_or_get(name = country)
Person_Country(person_id = person.id, country_id = country.id).save()

您能帮我确定问题是什么以及如何解决它吗?

注意:使用Sqlite3 3.8.9

最佳答案

嗯,外部 SELECT 语句的游标仍然打开,因此通过使用外部 SELECT ,您应该可以让代码正常工作:

for i in range(1, nb_rows // CHUNK_SIZE + 2):                                                                                                                                                                                                                                                                                                     
rows = (Source.select(Source.name,Source.country)
.where(Source.status == 'new').paginate(i, CHUNK_SIZE).tuples())

# CONSUME SELECT
rows = list(rows)

for name,country in rows:
person, created = Person.create_or_get(name = name, new = True)

if country:
country, created = Countries.create_or_get(name = country)
Person_Country(person_id = person.id, country_id = country.id).save()

create_or_get 将使用事务(如果您已经处于事务中,则使用保存点)。当提交或回滚时,SELECT 的游标将无法再使用。

关于python - 升级到 Peewee 2.6 后出现 sqlite3 InterfaceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31608858/

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