[파이썬] MySQL 데이터베이스 자동 커밋

MySQL is a popular open-source relational database management system used for storing and managing data. When working with MySQL databases in Python, it is essential to understand how transactions and commits work.

What is a transaction?

A transaction is a logical unit of work that consists of one or more database operations, such as INSERT, UPDATE, DELETE, or SELECT. By grouping these operations into a transaction, we can ensure that they are executed as a single, atomic unit. This means that either all the operations within the transaction are committed (saved to the database) or none of them are. This prevents data inconsistencies and maintains the integrity of the database.

What is a commit?

A commit is the action of permanently saving the changes made within a transaction to the database. Once a commit is performed, the changes become visible to other users accessing the database.

Auto-commit in MySQL

By default, MySQL operates in auto-commit mode, which means that each SQL statement is treated as a separate transaction and automatically committed once executed. However, in some cases, we may want to group multiple statements into a single transaction and manually control when the changes are committed.

In Python, we can use the mysql-connector-python library to connect to a MySQL database and execute SQL statements. To enable auto-commit mode, we set the autocommit attribute of the connection object to True:

import mysql.connector

# Connect to the MySQL database
cnx = mysql.connector.connect(user='username', password='password', host='localhost', database='mydb')

# Enable auto-commit
cnx.autocommit = True

# Execute SQL statements
# ...

# Close the connection
cnx.close()

In the above code snippet, once each SQL statement is executed, it will be automatically committed and saved to the database.

Disabling auto-commit

If we want to disable auto-commit and manually control when the changes are committed to the database, we can set the autocommit attribute of the connection object to False. This allows us to group multiple SQL statements into a single transaction and explicitly commit the changes using the commit() method:

import mysql.connector

# Connect to the MySQL database
cnx = mysql.connector.connect(user='username', password='password', host='localhost', database='mydb')

# Disable auto-commit
cnx.autocommit = False

# Create a cursor object
cursor = cnx.cursor()

# Execute SQL statements
# ...

# Commit the changes
cnx.commit()

# Close the cursor and the connection
cursor.close()
cnx.close()

In the above code snippet, the changes made by the SQL statements will only be committed to the database when we call the commit() method. If an error occurs or we decide not to commit the changes, we can roll back the transaction using the rollback() method:

# Rollback the transaction
cnx.rollback()

Conclusion

Understanding transactions and commits is crucial when working with MySQL databases in Python. By enabling or disabling auto-commit mode, we can control when changes are saved to the database. Auto-commit mode is suitable for simple operations, while disabling auto-commit gives us more flexibility to group statements into transactions and explicitly commit or roll back the changes.

Keep in mind that the code examples provided here are simplified and may need to be adapted based on your specific use case.