
Create a database with name mydb, under it create a table with name student with 3 fields i.e. rollno, name, and age, than run the code mentioned below:
Example 1: Input Student rollno, name, and age and insert into database using python code.
import mysql.connector
myconnect = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mydb"
)
mycursor = myconnect.cursor()
sql="insert into student values(%s ,%s , %s)"
rollno=int(input("Enter Rollno: "))
name=int(input("Enter Name: "))
age=int(input("Enter Age: "))
val=(rollno,name,age)
mycursor.execute(sql, val)
myconnect.commit()
print(mycursor.rowcount, "Record Inserted Successfully.")
Ad: