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.