Python SQLite – 如何手动BEGIN和END事务?
发布时间:2020-12-20 12:06:43 所属栏目:Python 来源:网络整理
导读:上下文 因此,我试图弄清楚在Python中使用SQLite时如何正确覆盖自动事务.当我试着跑 cursor.execute("BEGIN;").....an assortment of insert statements...cursor.execute("END;") 我收到以下错误: OperationalError: cannot commit - no transaction is act
上下文
因此,我试图弄清楚在Python中使用SQLite时如何正确覆盖自动事务.当我试着跑 cursor.execute("BEGIN;") .....an assortment of insert statements... cursor.execute("END;") 我收到以下错误: OperationalError: cannot commit - no transaction is active 我理解的是因为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") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |