With the intention to create a MySQL database in Python, you first have to provoke a connection utilizing the mysql.connector
. You possibly can find out about how one can create a connection to MySQL right here.
The best way to Create a Database in Python
Making a database is easy.
First, ensure you have an energetic connection to your database, after which set a cursor
.
Upon getting this, concern the execute
command to create your database.
import mysql.connector
mydb = mysql.connector.join(
host = "localhost",
consumer = "username",
password = "YoUrPaSsWoRd"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE your_database")
The best way to Verify if a MySQL Database Exists in Python
Utilizing an identical connection as described above, execute
the SHOW DATABASES
question.
import mysql.connector
mydb = mysql.connector.join(
host = "localhost",
consumer = "username",
password = "YoUrPaSsWoRd"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
The best way to Connect with a MySQL Database in Python
Now that you understand which database to hook up with, you’ll be able to specify it straight at first.
import mysql.connector
mydb = mysql.connector.join(
host = "localhost",
consumer = "username",
password = "YoUrPaSsWoRd",
database="your_database"
)