#include <mariadb/conncpp.hpp>
To connect to MariaDB databases from C++ applications, you need MariaDB Connector/C++.
Download the connector from the downloads page. You can find detailed installation instructions in the docs site.
Include the header file:
#include <mariadb/conncpp.hpp>
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));
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.
If you have questions, comments, or are looking for more information on MariaDB, please check out:
You can also reach out to us via: