mariadb-connector-c-usage

Guides writing and reviewing C application code using MariaDB Connector/C libmariadb APIs.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? C code generated against MariaDB Connector/C often contains subtle bugs: skipped mysql_init() calls, SQL injection from unescaped string concatenation, leaked result sets that break subsequent queries, wrong error-checking APIs, and unsafe thread sharing of connection handles. This Skill encodes the connector-specific behavior and traps so generated or reviewed C code uses libmariadb correctly. ## Core Features & Use Cases - API correctness rules: Covers the mysql_-prefixed public API, the mandatory mysql_init() → mysql_optionsv() → mysql_real_connect() sequence, and the buffered (mysql_store_result) vs unbuffered (mysql_use_result) result split with mandatory mysql_free_result(). - Prepared statements and transactions: Documents the ?-placeholder MYSQL_BIND workflow, bulk inserts via STMT_ATTR_ARRAY_SIZE array binding, one-shot mariadb_stmt_execute_direct(), and explicit mysql_autocommit(conn, 0) handling since autocommit is ON by default. - Safety guidance: Addresses SQL injection prevention with mysql_real_escape_string(), binary-safe mysql_real_query(), multi-result handling with mysql_next_result(), and per-thread connection rules with mysql_thread_init()/mysql_thread_end(). - Use Case: When asked to write a C program that inserts rows into MariaDB, the Skill produces code using prepared statements with array binding, proper error checks via mysql_stmt_errno(), and correct handle cleanup ordering. ## Quick Start Write C code that connects to MariaDB with Connector/C, runs a parameterized SELECT with a prepared statement, and performs a two-statement transaction with proper error handling.

Frequently Asked Questions about mariadb-connector-c-usage

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

FAQPage Schema
How do I connect to MariaDB from C using Connector/C?▼

Call mysql_init(NULL) to allocate a MYSQL handle, optionally set options with mysql_optionsv(), then call mysql_real_connect() with host, credentials, database, and port. Every other function except mysql_options() fails until mysql_real_connect() succeeds on that handle.

How do I use prepared statements with MariaDB Connector/C?▼

Use mysql_stmt_init(), mysql_stmt_prepare() with ? placeholders, fill a MYSQL_BIND array, then mysql_stmt_bind_param() and mysql_stmt_execute(). For results, bind a second MYSQL_BIND array with mysql_stmt_bind_result() and loop mysql_stmt_fetch() until it returns MYSQL_NO_DATA.

What is the difference between mysql_store_result and mysql_use_result?▼

mysql_store_result() is buffered, pulling the whole result set into client memory and enabling mysql_num_rows() and mysql_data_seek(). mysql_use_result() is unbuffered, fetching row-by-row and blocking the connection until all rows are consumed or the result is freed with mysql_free_result().

Does MariaDB Connector/C use a mariadb_-prefixed API?▼

No, the public API remains mysql_-prefixed (mysql_init, mysql_query, mysql_real_connect) for MySQL C API compatibility. Only a few extension functions use the mariadb_ prefix, such as mariadb_reconnect(), mariadb_get_info(), and mariadb_stmt_execute_direct().

Why does my next query fail after calling mysql_query?▼

A result-returning statement requires calling mysql_store_result() or mysql_use_result() and then mysql_free_result(), even when no rows are expected. Skipping this leaves the connection in a state where the next query fails.

Is a MariaDB Connector/C connection handle thread-safe?▼

No, a MYSQL handle is not thread-safe; use one connection per thread. Each thread must call mysql_thread_init() on entry and mysql_thread_end() on exit, with mysql_library_init() before the first connection and mysql_library_end() at shutdown.