標籤:top span doc library mos 模組 cti connect 文檔
Python 3.6.0的sqlite3模組存在一個bug(見issue 29003),無法執行VACUUM語句。
一執行就出現異常:
Traceback (most recent call last):
File "D:\desktop\cannot_vacuum.py", line 25, in <module>
conn.execute(‘VACUUM‘)
sqlite3.OperationalError: cannot VACUUM from within a transaction
這個bug也許會在Python 3.6.1中解決。
在Python 3.6.0,要想讓程式正常運行,需要在connect時設定isolation_level參數為None,如下:
conn = sqlite3.connect(‘test.db‘, isolation_level=None)
或者這樣做:
conn = sqlite3.connect(‘test.db‘)
conn.isolation_level = None
查看文檔,isolation_level=None表示自動commit:
If you want autocommit mode, then set isolation_level to None.
Otherwise leave it at its default, which will result in a plain “BEGIN” statement, or set it to one of SQLite’s supported isolation levels: “DEFERRED”, “IMMEDIATE” or “EXCLUSIVE”.
Changed in version 3.6: sqlite3 used to implicitly commit an open transaction before DDL statements. This is no longer the case.
如果保持isolation_level為預設值,則會在執行每條SQL語句之前,自動加上"BEGIN"語句,從而形成一個事務。
而VACUUM語句是不能在事務中執行的,因此出現了異常。
Python 3.6.0的sqlite3模組無法執行VACUUM語句