为什么在进行 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()
,则表不会更新。但对于 select 语句,我不需要 conn.commit()
。我很好奇为什么?
Here's my code:
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()
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DB-API 规范要求连接到数据库开始一个新事务,默认情况下。您必须
提交
以确认所做的任何更改,或回滚
以放弃它们。纯 SELECT 语句,因为它们从未对数据库进行任何更改,所以不必提交更改。
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, orrollback
to discard them.Pure
SELECT
statements, since they never make any changes to the database, don't have to have their changes committed.其他人已经解释了为什么 SELECT 语句不需要提交。我只是想指出您可以利用 Connection 对象以避免必须自己手动执行提交:
当同一连接中有多个 INSERT、UPDATE 和 DELETE 语句时,这尤其有用。
Others have explained why a commit is not necessary on a SELECT statement. I just wanted to point out you could utilize the
autocommit
property of the Connection object to avoid having to manually execute commit yourself:This is especially useful when you have multiple INSERT, UPDATE, and DELETE statements within the same connection.
commit用于告诉数据库保存当前事务中的所有更改。
Select 不会更改任何数据,因此无需保存任何内容,也无需提交任何内容。
请参阅 wikipedia 了解事务
commit is used to tell the database to save all the changes in the current transaction.
Select does not change any data so there is nothing to save and thus nothing to commit
See wikipedia for transactions