POD
POD

Reputation: 519

Connecting to MySQL From Eclipse (CDT)

I tried to connect to MySQL database by C++ in Eclipse with all the recommendations that are on the internet, but none of them are fully helpful. I am using Eclipse with MinGW to connect to MySQL. I have added the:

C:\Program Files\boost
C:\Program Files\MySQL\MySQL Connector C++ 1.1.3\include
C:\Program Files\MySQL\MySQL Server 5.6\include

to the include directory and

C:\Program Files\MySQL\MySQL Server 5.6\lib
C:\Program Files\MySQL\Connector C++ 1.1.2\lib\opt

to Library Directories (-L). Also I added the

libmysql.lib
mysqlcppconn-static.lib

to Additional Dependencies (-l).

The code is:

#include <stdlib.h>
#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;
using namespace sql::mysql;

int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;

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

  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
  /* Connect to the MySQL test database */
  con->setSchema("test");

  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
  while (res->next()) {
    cout << "\t... MySQL replies: ";
    /* Access column data by alias or column name */
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    /* Access column fata by numeric offset, 1 is the first column */
    cout << res->getString(1) << endl;
  }
  delete res;
  delete stmt;
  delete con;

} catch (sql::SQLException &e) {
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}

But none of them seems to work and lots of errors pops up. For now I get an error on "call of overloaded 'get_driver_instance()' is ambiguous" Does any of you guys have an experience to connect to MySQL through Eclipse in C++ environment. I am not a Linux user and the tutorial on Linux didn't work either.

Upvotes: 2

Views: 4591

Answers (1)

richar8086
richar8086

Reputation: 176

Just substitute this

driver = get_driver_instance();

by this

 driver = sql::get_driver_instance();

it's only a namespace problem;

Upvotes: 1

Related Questions