In this article, let's take a look at
Pythonmysql Transactions, for a friend who has just come into contact with Python's programming language, it may
python TransactionsThere is little understanding of control related aspects, and I do not understand what a Python transaction is, so in this article we will talk about the knowledge of pythonmysql transaction control.
What is a transaction
A transaction (Transaction) is a unit of concurrency control and is a user-defined sequence of operations. These operations are either done or not, and are an inseparable unit of work. With transactions, SQL Server can bind a logically related set of operations so that the server maintains the integrity of the data. A broad example of transactional applications is the deposit and withdrawal business in banks.
Transaction properties
Transaction mechanisms ensure data consistency.
A transaction should have 4 properties : atomicity, consistency, isolation, persistence. These four properties are often called acid properties.
1. atomicity (atomicity). A transaction is an inseparable unit of work, and the operations included in the transaction are either done or not.
2. Consistency (consistency). The transaction must be to change the database from one consistency state to another. Consistency is closely related to atomicity.
3. Isolation (Isolation). Execution of one transaction cannot be disturbed by other transactions. That is, the operations inside a transaction and the data used are isolated from other transactions that are concurrently executing, and cannot interfere with each other concurrently.
4. Persistence (durability). Persistence, also known as permanence (permanence), refers to the fact that once a transaction is committed, its changes to the data in the database should be permanent. The next operation or failure should not have any effect on it.
The transaction for Python DB API 2.0 provides two methods of commit or rollback.
Instance parsing
# SQL Delete Record statement sql = "Delete from EMPLOYEE WHERE age > '%d '" "%" try: # Execute SQL statement cursor.execute (SQL) # submit to Database C4/>db.commit () except: # rollback db.rollback () when an error occurs
For a database that supports transactions, in Python database programming, when the cursor is established, an invisible database transaction is automatically started.
Commit () method all the update operations of the cursor, the rollback () method rolls back all operations of the current cursor. Each method starts a new transaction.