In this tutorial, we will introduce the way to insert data into mysql table using python.
1. Import library
import mysql.connector
2. Connect mysql database
db_connection = mysql.connector.connect( host="localhost", user="your_username_here", passwd="your_password_here", database="my_test_db" )
This code will allow python to operate mysql database my_test_db
3. Insert data into mysql table
You can create a insert sql to insert data.
sql_statement = "INSERT INTO codespeedy (category,duration,level) values('python','10','1')"
Then you can execute this insert sql to insert data using python.
my_database.execute(sql_statement) db_connection.commit()