-2

Good morning, I am trying to connect to mysql in c++. I am working on macbook pro.

I use simple code from original docs.

When I try to compile my program it will fail.

main.cpp:15:10: fatal error: 'cppconn/driver.h' file not found
#include <cppconn/driver.h> 

I found here solution which doesn't work. Also solution in docs doesn't work too.

My code:

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
  Include directly the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#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(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 data 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;
}

I tried to install cppconn by

brew install mysql-connector-c++

Thank you for your help.

Amateur
  • 43
  • 6
  • 1
    How did you install mysql connector? Where did you put the header files? – drescherjm Jul 28 '20 at 18:39
  • Did you tell the compiler where to look for the headers. We can ask questions in comments all day. It's probably better if you write the question so we don't have to. – user4581301 Jul 28 '20 at 18:43
  • 1
    Side note: Writing a good question forces you to consider a lot of possibilities and often shakes lose the solution, so a good question is beneficial in many ways. You may find the answer yourself. If not, we can answer the question faster and more accurately. – user4581301 Jul 28 '20 at 18:46
  • Thank you for the additional information. I can't help with `brew`. I think you need to investigate where it put the headers and add that path to your compiler's include directory setting if it is in a location that is not the system default. – drescherjm Jul 28 '20 at 18:53

1 Answers1

0

I found full working solution. Just install package from this site.

Amateur
  • 43
  • 6