mariadb-connector-cpp-usage

Guides writing and reviewing C++ application code using MariaDB Connector/C++ sql:: API.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Generated or hand-written C++ code against MariaDB Connector/C++ often breaks on JDBC-style conventions: 1-based indexes, raw owning pointers, sql::SQLString types, and autocommit defaults. This Skill encodes those connector-specific behaviors so code compiles, runs, and manages resources correctly. ## Core Features & Use Cases - Correct API usage patterns: Obtain the driver via sql::mariadb::get_driver_instance(), connect with jdbc:mariadb:// URLs, and wrap returned raw pointers in std::unique_ptr. - Trap avoidance: Covers 1-based parameter and column indexes, mandatory ResultSet::next() before reads, ?-only placeholders, wasNull() checks, and sql::SQLException handling. - Transactions and batching: Shows setAutoCommit(false) with commit()/rollback(), savepoints, addBatch()/executeBatch(), and options like useServerPrepStmts and rewriteBatchedStatements. - Use Case: When reviewing a C++ service that inserts rows into MariaDB, use this Skill to catch a 0-based setString(0, ...) call, an unwrapped Connection* leak, and a missing commit() after disabling autocommit. ## Quick Start Ask the AI to write or review C++ code that connects to MariaDB using Connector/C++, for example a prepared-statement insert with proper pointer ownership and error handling.

Frequently Asked Questions about mariadb-connector-cpp-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++?▼

Get the driver singleton with sql::mariadb::get_driver_instance(), then call driver->connect(url, props) with a JDBC-style URL like jdbc:mariadb://host:3306/db. The call returns a raw Connection pointer you should wrap immediately in std::unique_ptr.

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

Call conn->prepareStatement() with positional ? placeholders only, then bind values with setString(1, ...), setInt(2, ...). Parameter indexes are 1-based, and client-side prepared statements are the default unless useServerPrepStmts=true is set.

Why does setString(0) or getString(0) throw an exception?▼

Both PreparedStatement parameter indexes and ResultSet column indexes are 1-based in Connector/C++. Index 0 or an out-of-range index throws sql::IllegalArgumentException, a subclass of sql::SQLException.

Does MariaDB Connector/C++ free Connection and ResultSet pointers automatically?▼

No. Factory methods like connect(), prepareStatement(), and executeQuery() return raw owning pointers with no automatic cleanup. Wrap them in std::unique_ptr or call close() explicitly, destroying children before the parent Connection.

How do I detect SQL NULL values in a ResultSet?▼

A SQL NULL reads back as the type's zero value, so call ResultSet::wasNull() immediately after getInt() or getString() to distinguish a real 0 from a NULL.

Is MariaDB Connector/C++ safe to share across threads?▼

get_driver_instance() returns a process-wide singleton safe for obtaining independent connections from multiple threads, but individual Connection, Statement, and ResultSet objects are not documented as thread-safe. Give each thread its own connection.