Update MySQL Table Data Using Python

In this tutorial, we will introduce the way to update database table in mysql server using python.

1. Import library

import mysql.connector

2. Connect mysql server

db_connection = mysql.connector.connect(
  host="localhost",
  user="put_username_here",
  passwd="put_password_here",
  database="database_name"
)
my_database = db_connection.cursor()

This code will connect mysql server and select a database to operate.

3. Create a update sql

You can create a update sql to update table data in mysql.

sql_statement = "UPDATE CODESPEEDY SET duration='150 Hours' where category='Python'"

4. Use python to execute sql statement

my_database.execute(sql_statement)
db_connection.commit()