MariaDB Connector/C++ is now a release candidate (RC). C++ is known for its efficiency, versatility and extensibility, and it is often used in industries like fintech where applications require predictable concurrency and latency. Now, C++ developers can use the native MariaDB Connector/C++ to connect their applications to MariaDB Server on premises and in the cloud on
MariaDB SkySQL.
MariaDB Connector/C++ is available from the
MariaDB Download page (choose “C++ connector”).
MariaDB Enterprise Documentation includes
instructions for installing and using Connector/C++.
MariaDB Connector/C++ is a release candidate (RC) release. It is not recommended for production usage.
MariaDB works closely with application developers and DBAs to ensure our Connectors meet requirements. We hope that you’ll try out this new connector, and welcome your feedback through
Jira, where open issues can be seen and new issues can be reported.
Since C++ doesn’t have a standard API for database access, the MariaDB Connector/C++ implements a custom C++ version of Java’s JDBC API.
Example
Create a database
test and a table
contacts to test basic operations:
CREATE DATABASE IF NOT EXISTS test;
CREATE TABLE test.contacts (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(25),
last_name VARCHAR(25),
email VARCHAR(100)) ENGINE=InnoDB;
Create a user
db_user that is to connect to MariaDB:
CREATE USER IF NOT EXISTS db_user@localhost IDENTIFIED BY
'db_user_password';
GRANT ALL PRIVILEGES ON *.* TO db_user@localhost WITH GRANT OPTION;
This sample code adds data to the
test.contacts table:
// Includes
#include<iostream>
#include<mariadbcpp/ConnCpp.h>
// Function to Add Contact
void addContact(std::shared_ptr<sql::PreparedStatement> &stmnt,
sql::SQLString first_name,
sql::SQLString last_name, sql::SQLString email) {
try {
// Bind Values
stmnt->setString(1, first_name);
stmnt->setString(2, last_name);
stmnt->setString(3, email);
// Execute Statement
stmnt->execute();
}
// Handle Exceptions
catch (sql::SQLException &e) {
std::cerr << "Error adding contact to database: " << e.what()
<< std::endl;
}
}
// Main Process
int main(int argc, char **argv) {
try {
// Instantiate Driver
sql::Driver* driver = sql::mariadb::get_driver_instance();
// Configure Connection
sql::SQLString url("jdbc:mariadb://localhost:3306/test");
sql::Properties properties({{"user", "db_user"},
{"password", "db_user_password"}});
// Establish Connection
std::unique_ptr<sql::Connection> conn(driver->connect(url,
properties));
// Prepare Statement
std::shared_ptr<sql::PreparedStatement>
stmnt(conn->prepareStatement("INSERT INTO test.contacts(first_name,
last_name, email) VALUES (?, ?, ?)"));
// Use prepared statement to add data
addContact(stmnt, "John", "Smith", "john.smith@example.com");
}
// Catch Exceptions
catch (sql::SQLException &e) {
std::cerr << "Error Connecting to MariaDB Platform: " << e.what() <<
std::endl;
// Exit (Failed)
return 1;
}
// Exit (Success)
return 0;
}
After running the C++ program to add data, run a SQL query in MariaDB CLI to verify that data has been added. Example output:
SELECT * from test.contacts;
+----+------------+-----------+------------------------+
| id | first_name | last_name | email |
+----+------------+-----------+------------------------+
| 1 | John | Smith | john.smith@example.com |
+----+------------+-----------+------------------------+
1 row in set (0.000 sec)
Ready to get started? See the
Connector/C++ Documentation for install instructions and more complete usage examples.