Peewee表示“无法提交-没有交易处于活动状态". [英] Peewee says "cannot commit - no transaction is active"
问题描述
我的CherryPy应用程序每小时使用以下代码进行一些清理:
My CherryPy app does some cleaning every hour with the following code:
def every_hour():
two_hours_ago = time.time() - 2 * 60 * 60
DbChoice.delete().where(DbChoice.time_stamp < two_hours_ago).execute()
monitor_every_hour = Monitor(cherrypy.engine, every_hour, frequency=60 * 60)
monitor_every_hour.start()
有时它会崩溃并显示以下消息:
Sometimes it crashes with he following message:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\peewee.py", line 2364, in execute_sql
self.commit()
File "C:\Python34\lib\site-packages\peewee.py", line 2371, in commit
self.get_conn().commit()
sqlite3.OperationalError: cannot commit - no transaction is active
此线程和其他人谈论如何解决直接与sqlite一起工作时会出现问题,但我使用的是Peewee,但我不知道我对Peewee做错了什么还是它是一个错误,我需要解决.
This thread and others talk about how to fix the problem when working directly with sqlite, but I am using Peewee and I don't know if I am doing something wrong with Peewee or it is a bug and I need to work around it.
我通过以下方式开始连接:
I start the connection with:
db = peewee.SqliteDatabase(path_name + '/doc.db', check_same_thread=False)
推荐答案
看起来默认情况下查询是自动提交的.因此,尝试将autocommit设置为False.
Looks like the query is automatically committed by default. So Try setting autocommit to False.
db = peewee.SqliteDatabase(path_name + '/doc.db', check_same_thread=False)
db.set_autocommit(False)
http://peewee.readthedocs.org/en/2.0.2/peewee/cookbook.html#changing-autocommit-behavior
希望这会有所帮助!
这篇关于Peewee表示“无法提交-没有交易处于活动状态".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!