Thursday, May 18, 2023
HomeSoftware EngineeringTips on how to Create a MySQL Database in Python

Tips on how to Create a MySQL Database in Python


With the intention to create a MySQL database in Python, you first have to provoke a connection utilizing the mysql.connector. You may find out about the way to create a connection to MySQL right here.

Tips on how to Create a Database in Python

Making a database is easy.

First, be sure to have an energetic connection to your database, after which set a cursor.

After you have this, subject the execute command to create your database.

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE your_database")

Tips on how to Test if a MySQL Database Exists in Python

Utilizing an analogous connection as described above, execute the SHOW DATABASES question.

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd"
)

mycursor = mydb.cursor()

mycursor.execute("SHOW DATABASES")

for x in mycursor:
  print(x)

Tips on how to Hook up with a MySQL Database in Python

Now that you understand which database to hook up with, you’ll be able to specify it immediately at first.

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database="your_database"
)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments