mariadb-connector-python-usage

Guides writing Python application code against MariaDB using the mariadb DB API 2.0 driver.

28|115|Updated Jan 28, 2025
One-click install
npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-connector-python-usage-mariadb-corporation
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mariadb-connector-python-usage
Source: https://github.com/mariadb-corporation/mariadb-docs/tree/main/agent-skills/granular/connectors/mariadb-connector-python-usage
Command: npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-connector-python-usage-mariadb-corporation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Generated or hand-written Python code often misuses MariaDB Connector/Python by assuming MySQL-driver conventions — wrong placeholder styles, missing commits, scalar parameters, or unbuffered cursors — causing silent data loss, runtime errors, and memory blowups. ## Core Features & Use Cases - Correct parameter binding: Enforces the default qmark (?) paramstyle, one-element tuples like (v,), and executemany() with lists of tuples for bulk operations. - Transaction and connection guidance: Covers autocommit-off-by-default behavior, explicit conn.commit()/rollback(), keyword-argument connections, and ConnectionPool usage (default size 5, max 64). - Error handling and cursor behavior: Documents the mariadb.Error exception hierarchy with errno/sqlstate, buffered vs. unbuffered cursors, binary=True prepared statements, and callproc() for stored procedures. - Use Case: When reviewing a Flask app whose INSERTs silently disappear, this Skill identifies the missing conn.commit() caused by autocommit being off by default and provides the corrected pattern. ## Quick Start Ask the AI to write or review Python code that connects to MariaDB with the mariadb module, such as a pooled connection performing parameterized inserts with proper commit handling.

Frequently Asked Questions about mariadb-connector-python-usage

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use parameterized queries with MariaDB Connector/Python?▼

Pass values as the second argument to execute() using qmark placeholders: cursor.execute("SELECT ... WHERE id = ?", (id,)). The default paramstyle is qmark, not %s, and a single parameter must be a one-element tuple with a trailing comma.

Why do my INSERT statements disappear in MariaDB Python code?▼

Autocommit is False by default in MariaDB Connector/Python, so uncommitted DML rolls back when the connection closes. Call conn.commit() after DML statements, or open the connection with autocommit=True.

MariaDB Connector/Python vs MySQLdb or PyMySQL — what is different?▼

The module is imported as mariadb and uses qmark (?) placeholders by default rather than %s. It is a C-extension DB API 2.0 driver with its own exception hierarchy under mariadb.Error, so code written for MySQL drivers needs adjustment.

How do I set up connection pooling with the mariadb Python module?▼

Create a pool with mariadb.ConnectionPool(pool_name="app", pool_size=5, ...) and borrow connections via pool.get_connection(). Calling conn.close() returns the connection to the pool; pool_size defaults to 5 with a maximum of 64.

Does MariaDB Connector/Python support server-side prepared statements?▼

Yes, but the text protocol is the default. Set binary=True on the cursor (conn.cursor(binary=True)) or, since version 2.0, on the connection to switch to the binary protocol with server-side prepared statements.

Why does fetching a large result set exhaust memory in Python MariaDB code?▼

Cursors are buffered by default, pulling the entire result into memory. For large results, create an unbuffered cursor with conn.cursor(buffered=False) and iterate row by row, noting that scroll() is unavailable on unbuffered cursors.