0

I'm having trouble with the simple task of creating a connection to a MySQL database in C++. No errors are printed except for the program return value, which is -1.

I'm using Eclipse Oxygen as the IDE, and the OS is Ubuntu 16.04. The MySQL connector version is 1.1.9., and it's the generic Linux version (glibc 2.12).

The connector files reside in my home folder. I've added the directory path of the "include" folder to Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes. I've also added the "lib" path and "mysqlcppconn" library name to Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries and Library search path, respectively. Finally, I've added the "lib" path into the LD_LIBRARY_PATH environment variable in Properties -> Run/Debug Settings -> (Launch configuration) -> Environment.

DBManager.cpp:

DBManager::DBManager() {
    try {
        std::cout << "Getting driver instance" << std:endl;
        driver = sql::mysql::get_mysql_driver_instance();
        std::cout << "Creating connection" << std::endl;
        connection = driver->connect("tcp://127.0.0.1:3306", "username", "password");
        std::cout << "Connected!" << std::endl; // NOT REACHING THIS LINE
    } catch(const SQLException& ex) {
        // these lines are not printed
        std::cout << "Exception!" << std::endl;
        std::cout << ex.what() << std::endl;
        exit(1);
    }
}

DBManager::~DBManager() {
    std::cout << "Deleting connection" << std::endl;
    delete connection;
}

DBManager.h

#ifndef DBMANAGER_H_
#define DBMANAGER_H_

#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/exception.h>

class DBManager {
public: 
    DBManager();
    virtual ~DBManager();
private:
    sql::mysql::MySQL_Driver* driver;
    sql::Connection* connection;
};

#endif

main.cpp

#include <iostream>
#include "DBManager.h"

int main(int argc, char* argv[]) {    

    std::cout << "Starting" << std::endl;

    DBManager* dbManager = new DBManager();
    // ... program logic not reached
    delete dbManager; // not reached

    return 0;
}
mkkekkonen
  • 1,704
  • 2
  • 18
  • 35
  • 1
    Impossible to tell from the tiny snippet you posted. Post a [mcve]. – rustyx Oct 25 '17 at 19:49
  • Your design is strange -- you have the `DBManager` constructor catching the exception instead of throwing the exception back to the client. Your `DBManager` object is thus not initialized properly, but still available to the user. – PaulMcKenzie Oct 25 '17 at 19:55
  • @RustyX Ok, I'll post more information. – mkkekkonen Oct 25 '17 at 19:58
  • @PaulMcKenzie That's a good point. I might have to rethink the design. – mkkekkonen Oct 25 '17 at 19:59
  • @mkkekkonen `catch(SQLException ex) ` -- Too much C# programming in your blood? In C++, catch exceptions by reference or const reference, not by value. That should be `catch(const SQLException& ex)`. [See this](https://stackoverflow.com/questions/2522299/c-catch-blocks-catch-exception-by-value-or-reference) – PaulMcKenzie Oct 25 '17 at 20:17
  • @PaulMcKenzie Thanks for the info, edited, though it had no effect on the output. – mkkekkonen Oct 25 '17 at 20:24
  • @mkkekkonen Instead of just `catch(const SQLException&)`, add a `catch(...)` to see if any exception is actually being thrown. – PaulMcKenzie Oct 25 '17 at 20:28
  • @PaulMcKenzie No exception seems to be thrown even with the ellipsis `(...)`. – mkkekkonen Oct 25 '17 at 20:34
  • @mkkekkonen So maybe there is nothing wrong. I would recommend first making sure you know how to use the API as-is before you start making class wrappers. If you can't get a simple program to work, wrapping non-working code in a class with destructors, exception handling, etc. just adds more complexity. – PaulMcKenzie Oct 25 '17 at 22:28

1 Answers1

0

I finally got it to work, I was using the generic Linux binaries first, which did not work. When I switched to using the binaries for Ubuntu, the program started to work.

enter image description here

mkkekkonen
  • 1,704
  • 2
  • 18
  • 35