Home >Backend Development >Python Tutorial >What is a python transaction? Four attributes help you understand things
In this article, let’s learn about pythonMYSQL transactions. For those who have just come into contact with the programming language python, they may have some control related aspects of python transactions I don’t know much about it and don’t understand what a python transaction is, so in this article we will talk about the relevant knowledge of pythonMYSQL transaction control.
What is a transaction
A transaction is a unit of concurrency control and a user-defined sequence of operations. Either all of these operations are done, or none of them are done, and they are an integral unit of work. Through transactions, SQL Server can bind a logically related set of operations together so that the server maintains data integrity. A broad example of transactional application is deposits and withdrawals in banks.
Transaction attributes
The transaction mechanism can ensure data consistency.
Transactions should have 4 attributes: atomicity, consistency, isolation, and durability. These four properties are often called ACID properties.
1.Atomicity. A transaction is an indivisible unit of work. All operations included in the transaction are either done or none of them are done.
2. Consistency. A transaction must change the database from one consistency state to another. Consistency and atomicity are closely related.
3. Isolation. The execution of a transaction cannot be interfered with by other transactions. That is, the operations and data used within a transaction are isolated from other concurrent transactions, and transactions executed concurrently cannot interfere with each other.
4. Durability. Continuity, also known as permanence, means that once a transaction is committed, its changes to the data in the database should be permanent. Subsequent operations or failures should not have any impact on it.
Python DB API 2.0 transactions provide two methods commit or rollback.
Example analysis
# SQL删除记录语句sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)try: # 执行SQL语句 cursor.execute(sql) # 向数据库提交 db.commit()except: # 发生错误时回滚 db.rollback()
For databases that support transactions, in Python database programming, when the cursor is created, an invisible database transaction is automatically started.
The commit() method performs all update operations on the cursor, and the rollback() method rolls back all operations on the current cursor. Each method starts a new transaction.
The above is all the content described in this article. This article mainly introduces the relevant knowledge of pythonMYSQL transactions. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of What is a python transaction? Four attributes help you understand things. For more information, please follow other related articles on the PHP Chinese website!