MySQL Connector C++ on Ubuntu 10.04 LTS

Posted by clsung on Tuesday, March 6, 2012

I’ve trying to use MySQL Connector C++ for my project, here the example code (retrieved from [official site][1]):

mysqlconn.cpp:

#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;

#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "root"
#define EXAMPLE_PASS "get your own password here"
#define EXAMPLE_DB "information_schema"

int main(int argc, const char **argv)
{
    const string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
    const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
    const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
    const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);

    try {
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;

        driver = get_driver_instance();         /* Create a connection */
        con = driver->connect("tcp://" + url + ":3306", user, pass);
        con->setSchema(database); /* Set the database */

        stmt = con->createStatement();
        res = stmt->executeQuery("SELECT 'Hello World!' AS _message");

        while (res->next())
        {
            cout << "\t... MySQL replies: ";
            cout << res->getString("_message") << endl;
            cout << "\t... MySQL says it again: ";
            cout << res->getString(1) << endl;
        }
        delete res;
        delete stmt;
        delete con;
    } catch (sql::SQLException &e) {
        /*
          The MySQL Connector/C++ throws three different exceptions:

          - sql::MethodNotImplementedException (derived from sql::SQLException)
          - sql::InvalidArgumentException (derived from sql::SQLException)
          - sql::SQLException (derived from std::runtime_error)
        */
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        /* Use what() (derived from std::runtime_error) to fetch the error message */
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;

        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

To identify which argument for linker is quite easy:

% ldconfig -p |grep mysql
        libmysqlcppconn.so.4 (libc6,x86-64) => /usr/lib/libmysqlcppconn.so.4
        libmysqlcppconn.so (libc6,x86-64) => /usr/lib/libmysqlcppconn.so
        libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib/libmysqlclient_r.so.16
        libmysqlclient.so.16 (libc6,x86-64) => /usr/lib/libmysqlclient.so.16

However, when I try to compile it, error happened (missing boost library)

% g++ mysqlconn.cpp -lmysqlcppconn
In file included from /usr/include/mysql_connection.h:15,
                 from mysqlconn.cpp:3:
/usr/include/cppconn/connection.h:16:29: error: boost/variant.hpp: No such file or directory
In file included from mysqlconn.cpp:3:
/usr/include/mysql_connection.h:16:32: error: boost/shared_ptr.hpp: No such file or directory
In file included from /usr/include/mysql_connection.h:15,
...

Quick fix:

% sudo apt-get install libboost-dev

Now run the sample code:

% g++ mysqlconn.cpp -lmysqlcppconn; ./a.out
... MySQL replies: Hello World!
... MySQL says it again: Hello World!

[1]: http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-tutorials.html "MySQL Connector/C++ Tutorials"