psycopg2 中的事务支持

Transaction support in psycopg2

阅读 psycopg2 关于它如何处理事务的文档(除了将其与 with 语句一起使用),我有点困惑。

通读 docs,我明白了

By default, Psycopg opens a transaction before executing the first command: if commit() is not called, the effect of any data manipulation will be lost.

假设以上说法正确

dbconn = psycopg2.connect(...)   cursor = dbconn.cursor()
cursor.execute("insert record into a") 
cursor.execute("insert record into b")

cursor.execute("insert record into c") // This throw integrity error.
cursor.execute("commit") // this logs "WARNING:  there is no transaction in progress"

ab 的数据操作会 丢失

但据我所知,这并没有发生在 PostgreSQL 上。我肯定在这里遗漏了一些东西,但现在我不确定是什么和在哪里。

您必须处于自动提交模式。

警告是在没有启动显式事务时发出 COMMIT 时得到的警告(PostgreSQL 默认处于自动提交模式!)。

使用像

这样的语句
print(dbconn.autocommit)

验证是否启用了自动提交。

要更改 属性,只需使用

dbconn.autocommit = False