Skip to content

Instantly share code, notes, and snippets.

@ranajahanzaib
Created March 18, 2019 15:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranajahanzaib/2262e3a08ab8136b390e69ff4d38487e to your computer and use it in GitHub Desktop.
Save ranajahanzaib/2262e3a08ab8136b390e69ff4d38487e to your computer and use it in GitHub Desktop.
Having Trouble with MySQL Connectors
#include <iostream>
#include <stdlib.h>
#include <mysql-cppconn/jdbc/mysql_connection.h>
#include <mysql-cppconn/jdbc/cppconn/driver.h>
#include <mysql-cppconn/jdbc/cppconn/exception.h>
#include <mysql-cppconn/jdbc/cppconn/resultset.h>
#include <mysql-cppconn/jdbc/cppconn/statement.h>
using namespace std;
int main() {
// clear the console screen
system("clear");
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
try{
sql::Driver *driver;
sql::Connection *conn;
sql::Statement *stmt;
sql::ResultSet *res;
// create connection
driver = get_driver_instance();
conn = driver->connect("tcp://127.0.0.1:3306", "root", "test");
// connect to MySQL datbase
conn->setSchema("R42");
stmt = conn->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 conn;
} 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;
}
@ranajahanzaib
Copy link
Author

[rj@vostro mysql-connector-tests]$ g++ main.cpp 
/usr/bin/ld: /tmp/ccnxy6ZJ.o: in function `main':
main.cpp:(.text+0x44): undefined reference to `get_driver_instance'
collect2: error: ld returned 1 exit status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment