World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Shashank Shanu
3 years ago
CREATE DATABASE database_name
CREATE DATABASE new_db;
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| logging |
| mydatabase |
| new_db |
| performance_schema |
| students |
| sys |
+--------------------+
23 rows in set (0.13 sec)
# import connector
import mysql.connector
#establish connection
connect = mysql.connector.connect(user='root', password='password', host='127.0.0.1')
#Create cursor object using the cursor() method
connect_cursor = connect.cursor()
# Drop database MYDATABASE if already exists.
connect_cursor.execute("DROP database IF EXISTS My_New_Database")
# query to create a database
query = "CREATE database MY_New_DATABASE";
#Create database
connect_cursor.execute(query)
#Retrieving the list of databases
print("List of databases: ")
connect_cursor.execute("SHOW DATABASES")
print(connect_cursor.fetchall())
#Closing the connection
connect.close()
List of databases:
[
('information_schema',),
('dbbug61332',),
('details',),
('exampledatabase',),
('my_new_database',),
('new_db',),
('mysql',),
('performance_schema',)
]