[英]peewee.OperationalError: unable to close due to unfinalized statements or unfinished backups
I want to write a script in python which will work in loop. 我想用python编写一个可以循环运行的脚本。 Script uses sqlite database which is referenced by peewee.
脚本使用peewee引用的sqlite数据库。 I can't put all code in here because it has a few hundreds of lines, but I'll show the part of my code written in peewee.
我不能将所有代码都放在这里,因为它有几百行,但是我将展示用peewee编写的代码部分。 When I run my code only once then everything works fine, although it must work for a few days, so it has to be running in loop.
当我只运行一次代码时,尽管它必须工作几天,但一切都可以正常工作,因此必须循环运行。 When I do the loop then I get this error in the second iteration:
当我执行循环时,我在第二次迭代中收到此错误:
peewee.OperationalError: Connection already opened.
peewee.OperationalError:连接已打开。
I tried to solve it by simply closing connection using this line: db.close() 我试图通过使用以下行简单地关闭连接来解决此问题:db.close()
But... then I get this: 但是...然后我得到这个:
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2677, in
close
self._close(self._state.conn)
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2683, in _
close conn.close() sqlite3.OperationalError: unable to close due to unfinalized statements or unfinished backups close conn.close()sqlite3.OperationalError:由于未完成的语句或未完成的备份而无法关闭
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "run.py", line 76, in <module>
main()
File "run.py", line 69, in main
db.close()
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2677, in
close
self._close(self._state.conn)
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2509, in
__exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 186, in
reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2677, in
close
self._close(self._state.conn)
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2683, in
_close
conn.close()
peewee.OperationalError: unable to close due to unfinalized statements or
unfinished backups
class PeeweeDatabase: PeeweeDatabase类:
def __init__(self):
db.connect()
@staticmethod
def create_tables():
with db:
db.create_tables([Model1, Model2, Model3])
@staticmethod
def save_Problem(view_name, id, link):
current_table = globals()[view_name]
try:
current_table.insert({
NewProblemCreated.ID: id,
NewProblemCreated.link: link,
NewProblemCreated.deliveryDate: 0,
NewProblemCreated.firstEncounter: datetime.now(),
NewProblemCreated.latestEncounter: datetime.now(),
NewProblemCreated.HowMuchTimesSent: 0,
NewProblemCreated.EncounteredBefore: False,
}).execute()
logger.info('Problem {} saved'.format(id))
except IntegrityError:
pass
@staticmethod
def update_latest_delivery(view_name, id):
current_table = globals()[view_name]
(current_table
.update(deliveryDate=datetime.now(), HowMuchTimesSent=current_table.HowMuchTimesSent+1)
.where(current_table.ID == pr_id)
.execute())
@staticmethod
def check_last_delivery(view_name, pr_id):
current_table = globals()[view_name]
res = (current_table
.select(current_table.deliveryDate)
.where(pr_id == current_table.prID)
.namedtuples()
)
return res[0][0]
Anyone faced this problem before? 有人遇到过这个问题吗?
Earlier I used SQL queries directly and had no problems but I wanted to use some ORM. 之前,我直接使用SQL查询,没有问题,但是我想使用一些ORM。
My guess is you're using an ancient version of SQLite on your system. 我的猜测是您在系统上使用旧版本的SQLite。 The python sqlite3 driver will use
sqlite3_close_v2
if SQLite is > 3.07, otherwise it uses sqlite3_close
, which is responsible for this issue. 如果SQLite> 3.07,则python sqlite3驱动程序将使用
sqlite3_close_v2
,否则它将使用sqlite3_close
,这是导致此问题的原因。
You can try exhausting any cursors and rolling-back any potentially open transactions. 您可以尝试用尽所有游标并回滚任何可能打开的事务。 You can also run in
autocommit
mode. 您也可以在
autocommit
模式下运行。
Well, I know already how to fix this... I have deleted db.connect and now everything works fine. 好吧,我已经知道如何解决此问题了……我删除了db.connect,现在一切正常。 I thought that I have tried this already but it seems I didn't.
我以为我已经尝试过了,但似乎没有。 I guess when im using ORM I should avoid to using sql commands and let ORM do it on it's own way.
我猜我在使用ORM时应该避免使用sql命令,而让ORM以自己的方式进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.