MySQL is an open-source relational database management system. Here we will be creating a Python script that will take user input and insert this to a MySQL database. You may also be interested in updating data, deleting data and retrieving data.
In this example the user input will consist of two variables which are name and age. The script also expects two columns which are ‘Name’ and ‘Age’ to already exist as part of our database table.
Full source code for this project can be found below:
import mysql.connector import time def InsertToDB(Name, Age): mydb = mysql.connector.connect( host="", user="", password="", database="") mycursor = mydb.cursor() sql = "INSERT INTO Table_1 (Name, Age) VALUES (%s, %s)" val = (Name, Age) 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: ") InsertToDB(Name, Age) print("Thank you! Next person please\n\n") time.sleep(2)