GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Commit & RollBack Operation in Python - GeeksforGeeks Commit & RollBack Operation in PythonLast 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() methodThe 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
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 ChangesThe following example demonstrates how to update a student’s age and commit the change to the database:
Output: ![]() Explanation:
2. rollback() methodThe 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
Example: Handling Errors with rollback()The following example demonstrates how to use the rollback() method to revert changes if a database operation fails:
Output: If the database transaction is successful the output will be,
If the database transaction fails the output is an error raised as,
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |