Below is the Python program which will show you how to connect to MariaDB. Here is the code:
# Module Imports
import mariadb
import sys
# Connect to MariaDB Platform
try:
conn = mariadb.connect(
user="user",
password="password",
host="host",
port=port,
database="database"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get Cursor
cur = conn.cursor()
try:
cur.execute("show databases;")
cur.execute("use test_db;")
cur.execute("show tables;")
for table in cur:
print(table)
except mariadb.Error as e:
print(f"Error: {e}")
# Close Connection
conn.close()
Comments
Post a Comment