
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Syntax
db.commit()
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 | +----------+---------+-----------+------------+
Example
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 password",database="database_name") cursor=db.cursor() query="UPDATE Students SET City='Kolkata' WHERE Name='Kriti'" cursor.execute(query) db.commit() query="SELECT * FROM Students" cursor.execute(query) for row in cursor: print(row) db.close()
The above code updates the city name of Kriti and commits this change to the database.
Output
(‘Karan’, 4 ,’Amritsar’ , 95) (‘Sahil’ , 6 , ‘Amritsar’ ,93) (‘Kriti’ , 3 , ‘Kolkata’ ,88) (‘Amit’ , 9 , ‘Delhi’ , 90) (‘Priya’ , 5 , ‘Delhi’ ,85)
NOTE
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.
- Related Articles
- What is the rollback() method in Python MySQL?
- Commit & RollBack Operation in Python
- Java Connection commit() method with example
- What is the fetchone() method? Explain its use in MySQL Python?
- What is 3-way merge or merge commit in Git?
- What is zfill() method in Python?
- What is random.uniform method in Python?
- What is weekday() method in python?
- What is prmonth() method in python?
- How can we perform COMMIT transactions inside MySQL stored procedure?
- COMMIT & Rollback Operations in Perl
- What the difference between commit() and apply() in Sharedpreferences in Android?
- What happens when we use COMMIT in MySQL stored procedure and one of the transaction, under START transaction, fails?
- What is usage of update method in Python dictionary?
- What is the groups() method in regular expressions in Python?
