Connecting to a MariaDB database from Python is straightforward with the mariadb
module. MariaDB is a popular open-source database that is often used as an alternative to MySQL. The mariadb
module is specifically designed to interface with MariaDB databases, providing efficient and easy-to-use methods for database interactions.
Install the MariaDB Connector: Ensure you have the MariaDB Connector module installed. You can install it using pip:bash
pip install mariadb
Database Connection Details: Ensure you have the necessary connection details such as host (server name), database name, user ID, and password.
mariadb
module.mariadb.connect()
function to create a connection to the MariaDB database. You will need to pass the connection parameters such as host, database, user, and password.connection.cursor()
method. This cursor is used to execute SQL queries.execute()
method of the cursor object to run your SQL query. You can pass your SQL query as a string to this method.fetchall()
, fetchone()
, or by iterating over the cursor.Here’s a step-by-step example demonstrating how to execute a simple SELECT
query to fetch all records from a table called Users
:
import mariadb
# Define the connection parameters
try:
connection = mariadb.connect(
user="your_username",
password="your_password",
host="your_host_name",
port=3306,
database="your_database_name"
)
# Create a cursor from the connection
cursor = connection.cursor()
# Execute the SQL query
cursor.execute("SELECT * FROM Users")
# Fetch and print all rows from the executed query
for row in cursor:
print(row)
except mariadb.Error as e:
print(f"Error connecting to MariaDB: {e}")
finally:
# Close the cursor and connection
if cursor:
cursor.close()
if connection:
connection.close()
user
, password
, host
, port
, and database
. Replace your_username
, your_password
, your_host_name
, and your_database_name
with your actual MariaDB credentials. The default port for MariaDB is 3306.mariadb.connect()
establishes the connection to the MariaDB database.connection.cursor()
creates a cursor object which is used to execute SQL commands.cursor.execute("SELECT * FROM Users")
executes the SQL query to select all records from the Users
table.for row in cursor:
loop iterates over the cursor to fetch and print each row from the query results.Parameterized Queries: For security reasons, especially to avoid SQL injection attacks, use parameterized queries when working with user inputs.
cursor.execute("SELECT * FROM Users WHERE UserID = ?", (user_id,))
Error Handling: Implement error handling using try-except blocks to manage potential database errors gracefully.
try:
connection = mariadb.connect(
user="your_username",
password="your_password",
host="your_host_name",
port=3306,
database="your_database_name"
)
cursor = connection.cursor()
cursor.execute("SELECT * FROM Users")
for row in cursor:
print(row)
except mariadb.Error as e:
print(f"Error connecting to MariaDB: {e}")
finally:
if cursor:
cursor.close()
if connection:
connection.close()