9951 explained code solutions for 126 technologies


python-mysqlHow do I use Python to manage MySQL database migrations?


Python is a great tool for managing MySQL database migrations. To get started, you will need to install the MySQL Connector/Python library. This library provides an API that allows you to interact with the MySQL database.

Once the library is installed, you can use the following example code to connect to the database and perform a migration:

import mysql.connector

# Connect to the database
db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="password"
)

# Create a cursor object
cursor = db.cursor()

# Execute the migration SQL statement
cursor.execute("ALTER TABLE table_name ADD COLUMN new_column VARCHAR(100)")

# Commit the changes to the database
db.commit()

# Close the connection
db.close()

The code above will connect to the MySQL database, create a cursor object, execute the migration SQL statement, commit the changes to the database, and then close the connection.

Code explanation

  • import mysql.connector - imports the MySQL Connector/Python library
  • db = mysql.connector.connect(...) - connects to the MySQL database
  • cursor = db.cursor() - creates a cursor object
  • cursor.execute("...") - executes the migration SQL statement
  • db.commit() - commits the changes to the database
  • db.close() - closes the connection

For more information, please refer to the MySQL Connector/Python documentation.

Edit this code on GitHub