A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/commit-rollback-operation-in-python/ below:

Commit & RollBack Operation in Python

Commit & RollBack Operation in Python

Last Updated : 30 Jun, 2025

In Python, the commit() and rollback() methods play an essential role in database transactions, ensuring atomicity, consistency, isolation, and durability (ACID properties). These methods help manage changes to the database in a reliable way, ensuring data integrity even in case of errors or failures.

In this article, we will focus on the use of commit() and rollback() methods for handling database transactions.

MySQL Table in used in this article:  

1. commit() method

The commit() method is used to save changes made to the database permanently. It confirms that all the changes executed during the current session, such as INSERT, UPDATE, or DELETE queries, should be saved in the database. Without commit(), any changes would be lost once the connection is closed.

Syntax 

comm.commit()

For a better understanding of the concept look into the code below followed by the code explanation. The below demonstration of the commit() method is performed on a MySQL database.

Example: Updating a Record and Committing the Changes

The following example demonstrates how to update a student’s age and commit the change to the database:

Python
import mysql.connector

# Connecting to the Database
mydb = mysql.connector.connect(
    host='localhost',
    database='College',
    user='root',
    password='your_password'  # Use your actual password
)

cs = mydb.cursor()

# SQL query to update the age of a student named 'Rishi Kumar'
statement = "UPDATE STUDENT SET AGE = 25 WHERE Name = 'Rishi Kumar'"

cs.execute(statement)

# Commit the changes to make them permanent in the database
mydb.commit()

# Disconnecting from the database
mydb.close()

Output: 

Explanation:

2. rollback() method

The rollback() method is used to undo changes made during the current transaction. If a condition arises where one is not satisfied with the changes made to the database or a database transaction fails, the rollback() method can be used to retrieve the original data that was changed through the commit() method.

Syntax

comm.rollback()

Example: Handling Errors with rollback()

The following example demonstrates how to use the rollback() method to revert changes if a database operation fails:

Python
import mysql.connector
from mysql.connector import Error

try:
    # Connecting to the Database
    mydb = mysql.connector.connect(
        host='localhost',
        database='College',
        user='root',
        password='your_password'  # Use your actual password
    )

    cs = mydb.cursor()

    # SQL query to update the age of a student named 'Rishi Kumar'
    statement = "UPDATE STUDENT SET AGE = 25 WHERE Name = 'Rishi Kumar'"

    cs.execute(statement)

    # Commit the changes to make them permanent
    mydb.commit()

    # Success message
    print("Database Updated!")

except mysql.connector.Error as error:
    # Error message if the transaction fails
    print(f"Database Update Failed! Error: {error}")
    
    # Rollback changes if there's an error
    mydb.rollback()

# Disconnecting from the database
mydb.close()

Output: 

If the database transaction is successful the output will be, 

Database Updated!

If the database transaction fails the output is an error raised as,  

Database Update Failed!



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4