Connecting to MariaDB databases using C++

To connect to MariaDB databases from C++ applications, you need MariaDB Connector/C++.

Install the connector

Download the connector from the downloads page. You can find detailed installation instructions in the docs site.

Including the API

Include the header file:

#include  <mariadb/conncpp.hpp>

Connecting to the database

Connect to a MariaDB database running on your machine:

sql::SQLString url("jdbc:mariadb://localhost:3306/db");
sql::Properties properties({{"user", "the_user"}, {"password", "the_password"}});
sql::Driver* driver= sql::mariadb::get_driver_instance();
std::unique_ptr<Connection> conn(driver->connect(url, properties));

Querying the database

Execute a SQL query:

std::shared_ptr<sql::Statement> statement(conn->createStatement());
std::unique_ptr<sql::ResultSet> result(statement->executeQuery("SELECT name, email FROM contact"));

while (result->next())
{
    std::cout
        << result->getString("name")
        << " "
        << result->getString("email")
        << std::endl;
}

Check the documentation and the Developer Code Central on GitHub for more information and examples.

Need more help?

If you have questions, comments, or are looking for more information on MariaDB, please check out:

You can also reach out to us via: