How to Create a MySQL Database in Python


In this example, we will learn how to create a MySQL database in Python.

Source Code

import mysql.connector

# connect to server
mydb = mysql.connector.connect(
    host="localhost",
    port=3306,
    user="yourusername",
    password="yourpassword"
)

# get a cursor
mycursor = mydb.cursor()

# execute a query
mycursor.execute("CREATE DATABASE mydatabase")

# If the above code is executed with no error, you have successfully created a database.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments