MySQL is an open-source relational database management system. In this post we will be updating MySQL data via a Python script. You may also be interested in inserting data, deleting data and retrieving data.
The script expects two columns which are ‘Name’ and ‘Age’ to already exist as part of our database table. In this example we are assuming the users name and age already exist within the database, using their name as a reference point we enable them with the ability to update their age.
Full source code for this project can be found below:
import mysql.connector
import time
def UpdateDB(Name, Age):
mydb = mysql.connector.connect(
host="",
user="root",
password="",
database="")
mycursor = mydb.cursor()
sql = "UPDATE Table_1 SET Age = %s WHERE Name = %s"
val = (Age,Name)
mycursor.execute(sql, val)
mydb.commit()
mycursor.close()
mydb.close()
while 1 == 1:
Name = input("Please Enter your Name: ")
Age = input("Please Enter your Age: ")
UpdateDB(Name, Age)
print("Thank you! Next person please\n\n")
time.sleep(2)