MySQL C++ Connector installation

1k views Asked by At

I'm writing an application in c++ which uses MySQL-database to store and collect data. The problem I ran into is that I cannot seem to get MySQL C++ Connector installed and working. The documentation is very unclear - IMO - and does not cover all the steps to install it so that I can import the lib into my project files.

What I've done already:

  • Searched the internet for help, no one good working answer found;
  • Try manually copying the lib files to /usr/local/libs/, works half now;]
  • Try to install the Boost library (required by the MySQL Connector), works, but still gives me compile errors.

So, as you can see I've tried a lot. I've changed my source code many times with some examples and possible fixes that I could find on the internet, but none seem to really work. You can examine my latest part of the code below.

I have no idea if the connector is working fine now, as it does build without any errors. But, when I execute the program all I get is the following error:

Segmentation fault: 11

I'm building/compiling on Mac OS X, so I need to install the connector on that machine. I'm using Sublime as IDE (I know it's not an IDE, but it works surprisingly well!) and g++ as compiler.

Also note that I'm a beginner (read as not and expert :)) at c++ programming, so I could be making some 'huge' mistakes in either my code or my procedure to install this connector.

I thank you for reading, and hope to hear some helpful answers!

Qlii256

Source code:

#include <iostream>

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

using namespace std;

int main()
{
    //sql::Driver *driver;  // Using mysql::MySQL_Driver instead, found this fix on stackoverflow
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *conn;
    sql::Statement *stmt;
    sql::ResultSet *res;
    char e;

    // Try a mysql db connection
    try
    {
        driver = sql::mysql::get_driver_instance();  // create new db instance

        conn = driver->connect("127.0.0.1:3306", "username", "password");  // Connect to the db

        conn->setSchema("database_name");  // Select database to use
    }
    catch ( sql::SQLException &e )
    {
        cout << "MySQL error occured: " << e.getSQLState() << endl;
    }

    cout << "We are connected!" << endl;

    return 0;
}
1

There are 1 answers

0
curtwphillips On

I found this old question while having a similar problem, also on a mac. My solution was to use brew.

brew install mysql-connector-c++