What is Python commit() method in MySQL?

The commit() method is one among the various methods in Python which is used to make the database transactions.

Here, we will discuss about the commit() method. The commit() method is used to confirm the changes made by the user to the database. Whenever any change is made to the database using update or any other statements, it is necessary to commit the changes. If we donot use the commit() method after making any changes to the database, the database will not not be updated and changes will not be reflected.



db refers to the database connection object.

Given below is an example to update value in a table and commit the changes to the database.

Steps invloved to update data and commit change made in a table using MySQL in python

  • import MySQL connector

  • establish connection with the connector using connect()

  • create the cursor object using cursor() method

  • create a query using the appropriate mysql statements

  • execute the SQL query using execute() method

  • commit the changes made using commit() method

  • close the connection

Suppose we have a table named “Student” as follows −

|    Name  |   Class |    City   |    Marks   |
|    Karan |    4    | Amritsar  |    95      |
|    Sahil |    6    | Amritsar  |    93      |
|    Kriti |    3    | Batala    |    88      |
|   Khushi |    9    | Delhi     |    90      |
|    Kirat |    5    | Delhi     |    85      |


Suppose, we have the above table of students and we want to update the city of Kriti from Batala to Kolkata. And commit the changes to the database.

import mysql.connector
db=mysql.connector.connect(host="your host", user="your username", password="your


query="UPDATE Students SET City='Kolkata' WHERE Name='Kriti'"

query="SELECT * FROM Students"

for row in cursor:

The above code updates the city name of Kriti and commits this change to the database.


(‘Karan’, 4 ,’Amritsar’ , 95)
(‘Sahil’ , 6 , ‘Amritsar’ ,93)
(‘Kriti’ , 3 , ‘Kolkata’ ,88)
(‘Amit’ , 9 , ‘Delhi’ , 90)
(‘Priya’ , 5 , ‘Delhi’ ,85)


The db.commit() in the above code is important. It is used to commit the changes made to the table. Without using commit(), no changes will be made in the table.