简体   繁体   中英

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. This will insert value 1,2 into a,b accordingly. 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.

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. 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.

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.

This isn't a Python or ODBC issue, it's a relational database issue.

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 . This allows you to make several changes serially that appear in the database simultaneously (when the commit is issued). 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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