Python / SQLite3:无法提交-没有事务处于活动状态 [英] Python/SQLite3: cannot commit - no transaction is active
问题描述
我正在尝试使用Python(传统的2.7)和SQLite(3)编写书本索引器
代码归结为以下SQL语句序列:
'从tag_dict中选择计数(*)'()
/ * [(30,)] * /
'从file_meta中选择计数(*)'()
/ * [(63613 ,)] * /
'开始交易'()
'从归档文件中选择name =?的ID'('158326-158457.zip',)
/ * [(20,) ] * /
'从文件中选择id,其中name =?和archive =?'('158328.fb2',20)
/ * [(122707,)] * /
'从file_meta中删除where file =?'(122707,)
'提交事务'()
#错误:无法提交-没有事务处于活动状态
隔离级别为'DEFERRED'('EXCLUSIVE'更好)。
我尝试使用connection.commit()而不是cursor.execute('commit') -没事。
- 当然,我已经搜索了stackoverflow和Net,但是找到的答案无关紧要。 $出于性能原因,b $ b
- 自动提交模式不可接受。
- 我一次只使用一个数据库文件。 / li>
- 我的代码在单线程中运行。
- 所有SQL执行都是通过单个函数完成的,这确保了我只使用一个光标一次打开。
那么,这里的交易出了什么问题?
如果我使用connection.commit()(否te:没有connection.begin方法!),那么我只是丢失了数据。
当然,我已经对数据库的文件权限进行了两次/三重/ d检查文件及其目录。
好吧,因为它经常发生,所以我在提出问题后一分钟就找到了解决方案。 / p>
作为新手,我无法在8个小时内回答自己的问题...
所以,答案就在这里:
解决方案是在此处找到并由唯一的想法组成:
在Python应用程序中请勿在非自动提交模式下使用BEGIN / COMMIT-仅使用db.commit()和db.rollback() !
听起来很奇怪,但是可以。
好,因为通常会在提出问题后一分钟找到解决方案。
解决方案是在此处找到,并且只有一个主意:
永远不要在Python应用程序的非自动提交模式下使用BEGIN / COMMIT-仅使用db.commit()和db.rollback()!
这听起来很奇怪,但是可以
I'm trying to code a book indexer using Python (traditional, 2.7) and SQLite (3).
The code boils down to this sequence of SQL statements:
'select count(*) from tag_dict' ()
/* [(30,)] */
'select count(*) from file_meta' ()
/* [(63613,)] */
'begin transaction' ()
'select id from archive where name=?' ('158326-158457.zip',)
/* [(20,)] */
'select id from file where name=? and archive=?' ('158328.fb2', 20)
/* [(122707,)] */
'delete from file_meta where file=?' (122707,)
'commit transaction' ()
# error: cannot commit - no transaction is active
The isolation level is 'DEFERRED' ('EXCLUSIVE' is no better).
I've attempted to use connection.commit() instead of cursor.execute('commit') - nothing useful happened.
- Sure, I've searched stackoverflow and the Net, but the answers found are irrelevant.
- Autocommit mode is unacceptable for performance reason.
- I use the only database file at a time.
- My code runs in single thread.
- All the SQL execution is being done via single function that ensures that I have no more than only one cursor open at a time.
So, what's wrong with transaction here?
If I use connection.commit() (note: there is no connection.begin method!), then I merely lose my data.
Sure, I've double/triple/d checked file permissions on the database file and its directory.
Well, as it often happens I found the solution just a minutes after posing the question.
As a newbie, I cannot answer my own question for 8 hours... So, the answer is now there:
The solution was found here and consists of the only idea:
Never use BEGIN/COMMIT in non autocommit mode in Python application - use db.commit() and db.rollback() only!
It sounds odd, but it works.
Well, as it often happens I found the solution just a minutes after posing the question.
The solution was found here and consists of the only idea:
Never use BEGIN/COMMIT in non-autocommit mode in Python application - use db.commit() and db.rollback() only!
It sounds odd, but it works.
这篇关于Python / SQLite3:无法提交-没有事务处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!