Installation des librairies

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libmysqlclient-dev libmariadb-dev-compat libmariadb-dev libmariadb3 libmariadb-dev libmysqlcppconn7v5 libmysqlcppconn-dev

Code du programme d’exemple main.cpp

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main(int argc, char *argv[])
{
    sql::Driver* driver;
    sql::Connection* con;
    sql::Statement* stmt;
    sql::ResultSet* res;

    driver = get_driver_instance();
    con = driver->connect("tcp://ip_bdd:port_bdd", "user_bdd", "password_bdd");
    con->setSchema("nom_bdd");

    stmt = con->createStatement();
    res = stmt->executeQuery("select * from truc");
    while (res->next()) {
       std::cout << "id = " << res->getInt(1);
       std::cout << ", champ_1_string = " << res->getString(2);
       std::cout << ", champ_2_int = " << res->getInt(9) << "\n";
    }
    return 0;
}

Script pour la compilation compil.sh

#!/bin/bash
g++ main.cpp -lmysqlcppconn -o executable -fcompare-debug-second

Compilation et exécution

./compil.sh
./executable

Leave a Reply