MySQL Create Database 🗄️
Creating a Database
To create a database in MySQL, use the "CREATE DATABASE" statement.
✨ Create Database
python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")🔍 Check if Database Exists
You can check if a database exist by listing all databases in your system by using the "SHOW DATABASES" statement:
python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)✨ Pro Tip
If the database already exists, you will get an error. To avoid this, you can use "CREATE DATABASE IF NOT EXISTS mydatabase".