Geo*_*lov 3 python sqlite transactions
上下文
因此,我试图弄清楚在Python中使用SQLite时如何正确覆盖自动事务.当我试着跑
cursor.execute("BEGIN;")
.....an assortment of insert statements...
cursor.execute("END;")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
OperationalError: cannot commit - no transaction is active
Run Code Online (Sandbox Code Playgroud)
我理解的是因为Python中的SQLite会自动在每个修改语句上打开一个事务,在这种情况下是一个INSERT.
题:
我试图通过每几千条记录做一次交易来加快我的插入速度. 如何克服交易的自动开放?
作为@CL.说你必须设置隔离级别None.代码示例:
s = sqlite3.connect("./data.db")
s.isolation_level = None
try:
c = s.cursor()
c.execute("begin")
...
c.execute("commit")
except:
c.execute("rollback")
Run Code Online (Sandbox Code Playgroud)