☞ To insert a record, we need to perform following steps :
Steps : Import mysql.connector module → Open a connection → Create cursor object → Execute a query →Save the changes using commit( ).
☞ Example :
import mysql.connector as m mydb = m.connect(host='localhost', user='root', password='in$pireweb$oft', database='school') cur = mydb.cursor() a = int(input("Enter an admission number : ")) b = input("Enter a name : ") c = input("Enter Date of Birth in [YYYY-MM-DD] format : ") d = input("Enter a gender [M/F] : ") e = float(input(" Enter your fees : ")) s="INSERT INTO student (Admno, Name, DOB, gender, Fees) VALUES( '{}', '{}', '{}', '{}', '{}' )" .format (a,b,c,d,e) cur.execute(s)OR s="INSERT INTO student (Admno, Name, DOB, gender, Fees) VALUES( %s, %s, %s, %s, %s)" rec=(a,b,c,d,e) cur.execute(s,rec) mydb.commit() print("Data inserted successfully!!!")
☞ Output :
Enter an admission number : 6
Enter a name : Ananya
Enter Date of Birth in [YYYY-MM-DD] format : 2010-10-20
Enter a gender [M/F] : F
Enter fees : 1550.25
Data inserted successfully!!!