Download our e-book of Introduction To Python
Neha Kumawat
3 years ago
CREATE DATABASE name_of_the_database
mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.04 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| logging |
| mydatabase |
| mydb |
| performance_schema |
| students |
| sys |
+--------------------+
26 rows in set (0.15 sec)
import mysql.connector
#establishing the connection
conn = mysql.connector.connect(user='root', password='password', host='127.0.0.1')
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Doping database MYDATABASE if already exists.
cursor.execute("DROP database IF EXISTS MyDatabase")
#Preparing query to create a database
sql = "CREATE database MYDATABASE";
#Creating a database
cursor.execute(sql)
#Retrieving the list of databases
print("List of databases: ")
cursor.execute("SHOW DATABASES")
print(cursor.fetchall())
#Closing the connection
conn.close()
List of databases:
[
('information_schema',),
('dbbug61332',),
('details',),
('exampledatabase',),
('mydatabase',),
('mydb',),
('mysql',),
('performance_schema',)
]