Python MySQL: Creating New Database

Once connected to the MySQL server, you can create new databases using the CREATE DATABASE SQL statement. This section will show you how to do that using Python.

Steps to Create a Database

To create a database, follow these steps:


import mysql.connector

# Establishing a connection
connection = mysql.connector.connect(
    host='localhost',
    user='root',
    password='yourpassword'
)
cursor = connection.cursor()
cursor.execute("CREATE DATABASE student_db")
print("Database created successfully")
connection.close()
            

Output

Database created successfully