- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 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.
(‘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.
- What is the rollback() method in Python MySQL?
- Java Connection commit() method with example
- Commit & RollBack Operation in Python
- What is 3-way merge or merge commit in Git?
- What is the fetchone() method? Explain its use in MySQL Python?
- 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 is usage of update method in Python dictionary?
- What is the groups() method in regular expressions in Python?
- How to tag a commit in git?
- Difference Between COMMIT and ROLLBACK in SQL