9951 explained code solutions for 126 technologies


python-mysqlHow do I commit changes to a MySQL database using Python?


To commit changes to a MySQL database using Python, you can use the MySQLdb module. This module provides an interface for connecting to a MySQL database server from Python.

The following example code demonstrates how to connect to a MySQL database and commit changes to it:

import MySQLdb

# Open database connection
db = MySQLdb.connect("hostname","username","password","database_name" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("UPDATE table_name SET field_name = 'value' WHERE condition")

# Commit your changes in the database
db.commit()

# disconnect from server
db.close()

The code consists of the following parts:

  1. Importing the MySQLdb module
  2. Connecting to the MySQL database using MySQLdb.connect()
  3. Creating a cursor object using db.cursor()
  4. Executing an SQL query using cursor.execute()
  5. Committing changes to the database using db.commit()
  6. Closing the connection to the database using db.close()

For more information about using the MySQLdb module, please refer to the MySQLdb documentation.

Edit this code on GitHub