In this tutorial, we will introduce the way to use mysql connector to connect a mysql server, then create a mysql table using python.
1. Import library
import mysql.connector
2. Connect mysql server
db_connection = mysql.connector.connect( host="localhost", user="your_username", passwd="your_password", database="your_db_name" )
This code will allow us to operate mysql database your_db_name.
3. Create a table inĀ your_db_name
We can use python to execute a create table sql statement to create a table in mysql.
my_database = db_connection.cursor() my_database.execute("CREATE TABLE TestTB (category VARCHAR(255), name VARCHAR(255))")