9951 explained code solutions for 126 technologies


python-mysqlHow do I update a MySQL database with variables using Python?


Updating a MySQL database with variables using Python can be done using the MySQL Connector/Python library. This library provides an API that allows developers to access and manipulate MySQL databases with Python.

The following example code shows how to update a MySQL database with variables using Python:

# Import the MySQL Connector/Python library
import mysql.connector

# Create a connection to the MySQL database
db = mysql.connector.connect(
    host="localhost",
    user="user",
    passwd="password",
    database="database"
)

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

# Create the UPDATE SQL statement
sql = """UPDATE table SET column1 = %s, column2 = %s WHERE id = %s"""

# Create the values to be updated
values = (variable1, variable2, id)

# Execute the UPDATE statement
cursor.execute(sql, values)

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

This code will update the specified columns in the table with the values provided in the values variable. The %s placeholders will be replaced with the actual values from the values variable.

The parts of this code are:

  • import mysql.connector - imports the MySQL Connector/Python library
  • db = mysql.connector.connect() - creates a connection to the MySQL database
  • cursor = db.cursor() - creates a cursor object
  • sql = """UPDATE table SET column1 = %s, column2 = %s WHERE id = %s""" - creates the UPDATE SQL statement
  • values = (variable1, variable2, id) - creates the values to be updated
  • cursor.execute(sql, values) - executes the UPDATE statement
  • db.commit() - commits the changes to the database

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

Edit this code on GitHub