MariaDB Connector/Python Beta Now Available

Python, an interpreted, high-level, general-purpose programming language, has been increasing in popularity over the past several years. First released in 1991, Python is now in its third major version.

Developers have an affinity for the language for a variety of reasons including its high level of readability and a plethora of features that allow completing complex tasks with relative ease. According to the Stack Overflow Developer Survey for 2019, Python has positioned itself within the top five most popular programming languages and is the second most loved language (behind Rust) among developers.

Introducing Connector/Python

MariaDB is pleased to announce the immediate availability of MariaDB Connector/Python beta! Connector/Python enables Python programs to access MariaDB databases using an API that is compliant with the Python DB API 2.0 (PEP-249). To optimize for performance, the new connector is written in C and leverages the MariaDB Connector/C client library for client-server communication.

Why a new client? Although there are existing clients that can be used to communicate with MariaDB, the motivation behind creating the MariaDB Python Connector is to provide a lightweight, very fast client that supports all MariaDB features beyond what MySQL offers.

Getting started

To begin using Connector/Python, you must fulfill the following prerequisites:

  • Install Python 3 (minimum supported version is 3.6)
  • Access a MariaDB 10.x instance
  • Install MariaDB Connector/C version 3.1.5 or newer

Once you’ve met the prerequisites, you’re ready to install the connector using:

$ pip3 install --pre mariadb

With Connector/Python installed, you can now use it within your Python code. Get started by creating a new Python file called “example.py” in a location of your choice.

Open the file and add the following to the first line to enable the usage of the connector:

import mariadb

Add the configuration values for the MariaDB database instance that you want to connect to.

config = {
    'host': 'localhost',
    'user': 'root',
    'password': 'secret',
}

You’ll be able to use that configuration to connect to MariaDB using mariadb.connect:

conn = mariadb.connect(**config)

To execute queries, you need a cursor based on the connection:

 cur = conn.cursor()

And you can close the connection when you’re done with it:

conn.close()

To bring everything together, the following script connects to a local database and executes a few queries. See our documentation for more information on queries.

import sys
import mariadb

config = {
    'host': 'localhost',
    'user': 'root',
    'password': 'secret',
}
 
try:
    conn = mariadb.connect(**config, database='test')
except mariadb.Error as err:
    print(err, file=sys.stderr)
    sys.exit(1)

cur = conn.cursor()

cur.execute("SHOW TABLES")
for (tbl,) in cur.fetchall(): # pre-fetch all data to free up the cursor
    print("\n===", tbl, "===\n")
    cur.execute(f"SELECT * FROM `{tbl}`")
    print([x[0] for x in cur.description]) # print field names (as a list)
    for row in cur: # using an iterator minimizes the memory used
        print(row) # print every row in this table (each as a tuple)

cur.execute("INSERT INTO sample VALUES (?, ?, ?)",
    (1, "A 'string' with single quotes.", '2020-01-01'))

conn.close()

Save the changes, open a terminal window, navigate to the location of “example.py” and run:

$ python3 example.py

What’s next

As this release of MariaDB Connector/Python is beta, we do not recommend using it in production.

Connector/Python is in active development and we anticipate releasing updates soon, so stay tuned! If you have questions, encounter issues, or would like to contribute to the development of Connector/Python you, can find the source code on GitHub. Documentation for MariaDB Connector/Python is available in MariaDB Enterprise Documentation.