Creating new databases

In this section of the tutorial, we will create the new database PythonDB.

Getting the list of existing databases

We can get the list of all the databases by using the following MySQL query.

snippet
>  show databases;

Example

snippet
import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")

#creating the cursor object
cur = myconn.cursor()

try:
    dbs = cur.execute("show databases")
except:
    myconn.rollback()
for x in cur:
    print(x)
myconn.close()

Output:

Output
('EmployeeDB',) ('Test',) ('TestDB',) ('information_schema',) ('rookienerd',) ('rookienerd1',) ('mydb',) ('mysql',) ('performance_schema',) ('testDB',)

Creating the new database

The new database can be created by using the following SQL query.

snippet
>  create database <database-name>

Example

snippet
import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")

#creating the cursor object
cur = myconn.cursor()

try:
    #creating a new database
    cur.execute("create database PythonDB2")

    #getting the list of all the databases which will now include the new database PythonDB
    dbs = cur.execute("show databases")
    
except:
    myconn.rollback()

for x in cur:
        print(x)
        
myconn.close()

Output:

Output
('EmployeeDB',) ('PythonDB',) ('Test',) ('TestDB',) ('anshika',) ('information_schema',) ('rookienerd',) ('rookienerd1',) ('mydb',) ('mydb1',) ('mysql',) ('performance_schema',) ('testDB',)
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +