升级到Peewee 2.6后的sqlite3 InterfaceError

时间:2015-07-24 10:59:49

标签: python sqlite peewee

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

编辑:这似乎是与使用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

1 个答案:

答案 0 :(得分:1)

好吧,外部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的光标。