Python MySQL Create Table

Creating a Table

To create a table in MySQL, use the “CREATE TABLE” statement.

Make sure you define the name of the database when you create the connection

Example

Create a table named “customers”:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")

If the above code was executed with no errors, you have now successfully created a table. Continue reading Python MySQL Create Table