简体   繁体   中英

Python3 sqlite3 BEGIN IMMEDIATE error

I have some code that used to work, but after upgrading to Python 3.6.0 on Windows I am getting errors. I can confirm that the same code works just fine in Python 3.5.2. I narrowed the issue down to the following very simple code in which I am trying to explicitly set a writer lock on the database:

>>> import sqlite3
>>> conn = sqlite3.connect('testDB.db')
>>> cur = conn.cursor()
>>> conn.in_transaction
False
>>> cur.execute('BEGIN IMMEDIATE')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: cannot start a transaction within a transaction

The same code has no issues with Python 3.5.2:

>>> cur.execute('BEGIN IMMEDIATE')
<sqlite3.Cursor object at 0xb710ab60>

I tried to use conn = sqlite3.connect('testDB.db', isolation_level='IMMEDIATE') without success. Note that I am not doing any transaction such as INSERT or even SELECT.

UPDATE: I noticed the following on the Python sqlite3 documentation :

"Changed in version 3.6: sqlite3 used to implicitly commit an open transaction before DDL statements. This is no longer the case."

What does this mean and how to fix my code so that I can still explicitly lock the database?

If you want to use Python's automatic transaction handling, leave isolation_level at its default value, or set it to one of the three levels.

If you want to do your own transaction handling, you have to prevent Python from doing its own by setting isolation_level to None .

It's a bug. It will be fixed in Python 3.6.1: https://bugs.python.org/issue28518

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM