Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In an effort to create a MySQL database in Python, you first have to provoke a connection utilizing the mysql.connector
. You possibly can study how one can create a connection to MySQL right here.
Making a database is easy.
First, be sure to have an energetic connection to your database, after which set a cursor
.
Upon getting this, difficulty 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")
Utilizing the same 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)
Now that you already know which database to connect with, you may specify it instantly to start with.
import mysql.connector
mydb = mysql.connector.join(
host = "localhost",
consumer = "username",
password = "YoUrPaSsWoRd",
database="your_database"
)