Creating a table


☞ To create a table, we need to perform following steps :

Steps : Import mysql.connector module → Open a connection → Create cursor object → Execute a query.

Example :

import mysql.connector as m
mydb = m.connect(host='localhost', user='root', password='123', database='school')
cur = mydb.cursor( )

s= “CREATE TABLE IF NOT EXISTS student(Admno INTEGER(10) PRIMARY KEY, Name VARCHAR(30), DOB DATE, Gender CHAR(1), Fees DECIMAL(10,2))”
cur.execute(s)

#following lines are to show the result in python shell
cur.execute(“SHOW TABLES”)
for i in cur:
     print(i)

Output :

(‘student’,)
NOTE : You can also see the result in MySQL database.