Home >>Python Tutorial >Python MySQL Create Table
import mysql.connector
mydb = mysql.connector.connect (
host=“localhost”
user=“yourusername”
passwd=“yourpassword”
database=“mydatabase”
)
mycursor = mydb.cursor ( )
mycursor.execute (“CREATE TABLE customers (name VARCHAR(255), address VARCHAR (255)” )
While executing this code that is mentioned above, if you see no error popping up while completing the task of python create table mysql then that means that you have successfully crated a table.
import mysql.connector mydb = mysql.connector.connect ( host=“localhost” user=“yourusername” passwd=“yourpassword” database=“mydatabase” ) mycursor = mydb.cursor ( ) mycursor.execute (“SHOW TABLES”) for x in mycursor : print ( x )
import mysql.connector mydb = mysql.connector.connect ( host=“localhost” user=“yourusername” passwd=“yourpassword” database=“mydatabase” ) mycursor = mydb.cursor ( ) mycursor.execute (“CREATE TABLE customers ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR (255), address VARCHAR (255)” ) If the table that you were planning to use already exists then you need to use the ALTER TABLE keyword to perform the same task. import mysql.connector mydb = mysql.connector.connect ( host=“localhost” user=“yourusername” passwd=“yourpassword” database=“mydatabase” ) mycursor = mydb.cursor ( ) mycursor.execute (“ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY” )With this, we finish the pymysql create table part of our Python MySQL tutorial.