Connecting to MariaDB databases using Python

To connect to MariaDB databases from Python applications, you need MariaDB Connector/C and MariaDB Connector/Python.

Install the connectors

MariaDB Connector/C is a required dependency. Download it and install it from the downloads page.

Install MariaDB Connector/Python using PIP:

pip3 install mariadb

Connecting and querying

Connect to a local MariaDB database runing on your machine and execute a SQL query:

import mariadb

connection = mariadb.connect(
    host="localhost",
    user="user",
    port=3306,
    password="password",
    database="database_name")
cursor = connection.cursor()

cursor.execute("SELECT name, email FROM contact")

for name, email in cursor:
    print(f"{name} {email}")

connection.close()

Check the documentation and the Developer Code Central on GitHub for more information and examples.

Need more help?

If you have questions, comments, or are looking for more information on MariaDB, please check out:

You can also reach out to us via: