Python 3.6.0的sqlite3模組無法執行VACUUM語句

來源:互聯網
上載者:User

標籤: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語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.