简体   繁体   English

任何人都可以告诉我python pyodbc中connection.commit()的意义是什么?

[英]Can anyone tell me what' s the point of connection.commit() in python pyodbc ?

I used to be able to run and execute python using simply execute statement. 我以前只能使用execute语句运行和执行python。 This will insert value 1,2 into a,b accordingly. 这将相应地将值1,2插入a,b中。 But started last week, I got no error , but nothing happened in my database. 但是从上周开始,我没有收到任何错误,但我的数据库中没有任何反应。 No flag - nothing... 1,2 didn't get insert or replace into my table. 没有标志 - 没有... 1,2没有插入或替换到我的表。

connect.execute("REPLACE INTO TABLE(A,B) VALUES(1,2)")

I finally found the article that I need commit() if I have lost the connection to the server. 如果我丢失了与服务器的连接,我终于找到了需要commit()的文章。 So I have add 所以我补充一下

connect.execute("REPLACE INTO TABLE(A,B) VALUES(1,2)")
connect.commit()

now it works , but I just want to understand it a little bit , why do I need this , if I know I my connection did not get lost ? 现在它可以工作,但我只是想了解一点,为什么我需要这个,如果我知道我的连接没有丢失?

  • New to python - Thanks. python的新手 - 谢谢。

Not commiting puts all your queries into one transaction which is safer (and possibly better performance wise) when queries are related to each other. 不提交将所有查询放入一个事务中,当查询彼此相关时,这个事务更安全(并且可能更好地表现性能)。 What if the power goes between two queries that doesn't make sense independently - for instance transfering money from one account to another using two update queries. 如果权力在两个独立的查询之间进行,例如使用两个更新查询将资金从一个帐户转移到另一个帐户,该怎么办?

You can set autocommit to true if you don't want it, but there's not many reasons to do that. 如果不需要,可以将autocommit设置为true,但这样做的原因并不多。

This isn't a Python or ODBC issue, it's a relational database issue. 这不是Python或ODBC问题,而是关系数据库问题。

Relational databases generally work in terms of transactions: any time you change something, a transaction is started and is not ended until you either commit or rollback . 关系数据库通常在事务方面起作用:每当您更改某些内容时,事务就会启动,并且在您commitrollback之前不会结束。 This allows you to make several changes serially that appear in the database simultaneously (when the commit is issued). 这允许您同时进行多次连续出现的数据更改(发出commit时)。 It also allows you to abort the entire transaction as a unit if something goes awry (via rollback ), rather than having to explicitly undo each of the changes you've made. 如果出现问题(通过rollback ),它还允许您作为一个单元中止整个事务,而不是必须显式撤消您所做的每个更改。

You can make this functionality transparent by turning auto-commit on, in which case a commit will be issued after each statement, but this is generally considered a poor practice. 您可以通过启用自动提交来使此功能透明,在这种情况下,将在每个语句之后发出commit ,但这通常被认为是一种不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM