To create a new MySQL database in Python, you can use the mysql-connector-python
library to connect to the MySQL server and issue a CREATE DATABASE
statement.
The basic syntax of the create database statement:
CREATE DATABASE database_name
Here is an example:
import mysql.connector
mydb = mysql.connector.connect(
host="hostname",
user="username",
password="password"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
In this example, host
, user
, and password
should be replaced with the appropriate values for your MySQL server. Also, mydatabase
should be replaced with the desired name for the new database.
To list all databases in a MySQL server using Python, you can use the mysql-connector-python
library to connect to the MySQL server and issue a SHOW DATABASES
statement.
import mysql.connector
mydb = mysql.connector.connect(
host="hostname",
user="username",
password="password"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
In this example, host
, user
, and password
should be replaced with the appropriate values for your MySQL server.
The for loop will iterate through the results of the SHOW DATABASES
statement and print the name of each database.
Note: you need to have appropriate privileges to list all databases, otherwise it will give an error.
Note: also read about MySQL with Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…
Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…