How to insert a record into a table Using Python with MySQL

#insert records into employee tables
import mysql.connector

con=mysql.connector.connect(host='localhost',user='root',password='silan', database='silandb')

obj=con.cursor()

insert_record="insert into employee(ename,esal) values(%s,%s)"
tuple1=("Sreedha",30000)

obj.execute(insert_record,tuple1)

con.commit()

print(obj.rowcount, "row inserted.")



Output:



#insert multiple records into employee tables
import mysql.connector

con=mysql.connector.connect(host='localhost',user='root',password='silan', database='silandb')

obj=con.cursor()

insert_record="insert into employee(ename,esal) values(%s,%s)"
tuple2=[("Sanghamitra",30000),("Sneha",20000), ]

obj.executemany(insert_record,tuple2)

con.commit()

print(obj.rowcount, "row inserted.")



Output:


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext