xxxxxxxxxx
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pets |
| sys |
+--------------------+
5 rows in set (0.00 sec)
xxxxxxxxxx
import mysql.connector
# Connect to MySQL Server
connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password"
)
# Get list of all databases
cursor = connection.cursor()
cursor.execute("SHOW DATABASES")
# Print the list of databases
for database in cursor:
print(database[0])
# Close the cursor and connection
cursor.close()
connection.close()
xxxxxxxxxx
import mysql.connector
# Connect to the MySQL server
cnx = mysql.connector.connect(user='<username>', password='<password>',
host='<host>', database='<database>')
# Create a cursor object to execute the SQL queries
cursor = cnx.cursor()
# Execute the "SHOW DATABASES" query
cursor.execute("SHOW DATABASES")
# Fetch all the rows returned by the query
databases = cursor.fetchall()
# Print the names of the databases
for db in databases:
print(db[0])
# Close the cursor and connection
cursor.close()
cnx.close()