sqlite3连接用作上下文管理器的事务不是原子的 - python

According to the documentation,

连接对象可用作自动提交或回滚事务的上下文管理器。发生异常时,事务将回滚;否则,将提交事务:

我知道with语句中的所有内容都应该是原子事务。现在考虑这段代码

import sqlite3
con = sqlite3.connect(':memory:')

try:
  with con:
    con.execute('create table foo (id integer primary key)')
    con.execute('insert into foo values (1)')
    con.execute('insert into foo values (1)')
except sqlite3.Error:
  print('transaction failed')

try:
  rec = con.execute('select count(*) from foo')
  print('number of records: {}'.format(rec.fetchone()[0]))
except sqlite3.Error as e:
  print(e)

哪个返回

transaction failed
number of records: 0

一方面,由于值重复,交易失败。另一方面,表foo存在,即使它为空,这也意味着第一个插入已回滚。是否也应该回滚表的创建?

手动进行交易会产生预期的结果:

import sqlite3
con = sqlite3.connect(':memory:')

con.execute('begin')
try:
  con.execute('create table foo (id integer primary key)')
  con.execute('insert into foo values (1)')
  con.execute('insert into foo values (1)')
  con.execute('commit')
except sqlite3.Error:
  con.execute('rollback')
  print('transaction failed')

try:
  rec = con.execute('select count(*) from foo')
  print('number of records: {}'.format(rec.fetchone()[0]))
except sqlite3.Error as e:
  print(e)

退货

transaction failed
no such table: foo

为什么会出现差异?

参考方案

从Python 3.6开始,DDL或Data Defitinion Language语句(如CREATE TABLE)不会启动事务。这意味着任何这样的语句在您执行一个语句后即自动提交。

请参见Controlling transactions section:

默认情况下,sqlite3模块在数据修改语言(DML)语句(即INSERT / UPDATE / DELETE / REPLACE)之前隐式打开事务。

[...]

在版本3.6中更改:sqlite3用于在DDL语句之前隐式提交一个打开的事务。这已不再是这种情况。

这意味着如果您希望DDL语句成为事务的一部分,则必须显式启动事务。

将连接用作上下文管理器,仍然仅在退出时发出提交或回滚,而不会启动事务。相反,遇到的第一个DML语句将以1开始。如果要使DDL成为事务的一部分,请在顶部添加begin语句:

try:
    with con:
        con.execute('begin')  # explicit, rather than implicit, transaction start
        con.execute('create table foo (id integer primary key)')
        con.execute('insert into foo values (1)')
        con.execute('insert into foo values (1)')
except sqlite3.Error:
    print('transaction failed')

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

python JSON对象必须是str,bytes或bytearray,而不是'dict - python

在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…