为什么在执行UPDATE时需要显式提交? [英] Why the need to commit explicitly when doing an UPDATE?

查看:167
本文介绍了为什么在执行UPDATE时需要显式提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

import cx_Oracle

conn = cx_Oracle.connect(usr, pwd, url)
cursor = conn.cursor()
cursor.execute("UPDATE SO SET STATUS='PE' WHERE ID='100'")
conn.commit()

如果我删除了 conn.commit(),该表未更新。但是对于选择语句,我不需要 conn.commit()。我很好奇为什么?

If I remove the conn.commit(), the table isn't updated. But for select statements, I don't need that conn.commit(). I'm curious why?

推荐答案

DB-API 规范要求,默认情况下,连接到数据库会开始新的事务。您必须提交确认所做的任何更改,或回滚放弃它们。

The DB-API spec requires that connecting to the database begins a new transaction, by default. You must commit to confirm any changes you make, or rollback to discard them.


请注意,如果数据库支持自动提交功能,则必须首先将其关闭。

Note that if the database supports an auto-commit feature, this must be initially off.

SELECT 语句,因为它们从不对数据库进行任何更改,所以不必提交更改。

Pure SELECT statements, since they never make any changes to the database, don't have to have their changes committed.

这篇关于为什么在执行UPDATE时需要显式提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆