To create a database in MySQL using Python, you can use the `
mysql-connector-python
` module which provides a Python interface to
MySQL. Here's an example Python program that creates a database in MySQL :
Syntax :
import mysql.connector
# Create a connection to the MySQL server
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
# Create a cursor object to execute SQL statements
mycursor = mydb.cursor()
# Create a database
mycursor.execute("CREATE DATABASE mydatabase")
# Print a message to indicate the database has been created
print("Database created successfully!")​