Assuming that MySQL database named as test is present on server and a table named employee is also created. The table has five fields fname, lname, age, gender, and salary.
A tuple object containing data of a record is defined as
t1=('Mac', 'Mohan', 20, 'M', 2000)
To establish interface between MySQL and Python 3, you need to install PyMySQL module. Then you can set up the connection using following statements
import PyMySQL # Open database connection db = PyMySQL.connect("localhost","root","","test" ) # prepare a cursor object using cursor() method cursor = db.cursor()
Next step is to set up the insert query using data in the tuple
sql="insert into employee values(%s,%s,%d,%s,%d)" %t1
Now this query is executed using execute() method of cursor object
cursor.execute(sql)
If you check contents of employee table, it will show new record added in it.