Creating a Database


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

Step 1 : Import mysql.connector module and open a connection

Step 2 : Create cursor object

Step 3 : Execute a query

Example :

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

cur = mydb.cursor()    #creating cursor object
cur.execute(β€œCREATE DATABASE IF NOT EXISTS school”)      #executing a query

#following lines are to show the result in python shell
cur.execute(β€œSHOW DATABASES”)
for i in cur:
      print(i)

Output :

(β€˜information_schema’,)
(β€˜school’,)
(β€˜mysql’,)
(β€˜office’,)
(β€˜performance_schema’,)

Note : You can also see the result in MySQL database.