Rocco Galati
asked on
How to connect to MySQL with C++ and mysql++
I'm trying to retrieve data from my remote MySQL database from C++ under Ubuntu.
The problem is that I'm not able to get any result and it seems that the connection can't be established.
I compile it with:
This is the output:
I checked the connection data (IP, name, password) and they are OK, so it is not a problem with the authentication.
Can you help me, please?
I can't understand why it is not working.
P.S. I'm not forced to use mysql++, I can use any other library without problems.
#include <mysql++.h>
#include <stdlib.h>
using namespace std;
using namespace mysqlpp;
int main() {
cout << "Trying to connect..\n";
try {
Connection conn(false);
conn.connect("Sql737674_5", "62.149.xxx.xxx", "Sql737674", "xxxxxxxxx");
Query query = conn.query();
if (conn.connected()){
cout << "OK\n";
}
else {
cout << "NOT CONNECTED\n";
}
cout << "Eseguo Select\n";
/* Now SELECT */
query << "SELECT * FROM Utenti LIMIT 10";
StoreQueryResult ares = query.store();
cout << "Ciclo FOR\n";
// printf("numero: %d", ares.num_rows());
for (size_t i = 0; i < ares.num_rows(); i++){
cout << "Name: " << ares[i]["Nome"] << " - Cognome: " << ares[i]["Cognome"] << endl;
}
} catch (BadQuery er) { // handle any connection or
// query errors that may come up
cerr << "Error: " << er.what() << endl;
return -1;
} catch (const BadConversion& er) {
// Handle bad conversions
cerr << "Conversion error: " << er.what() << endl <<
"\tretrieved data size: " << er.retrieved <<
", actual size: " << er.actual_size << endl;
return -1;
} catch (const Exception& er) {
// Catch-all for any other MySQL++ exceptions
cerr << "Error: " << er.what() << endl;
return -1;
}
return (EXIT_SUCCESS);
}
The problem is that I'm not able to get any result and it seems that the connection can't be established.
I compile it with:
g++ -I/usr/include/mysql -I/usr/local/include/mysql++ -L/usr/lib test.cpp -lmysqlpp -lmysqlclient -lnsl -lz -lm -o test
This is the output:
Trying to connect..
NOT CONNECTED
Eseguo Select
Ciclo FOR
I checked the connection data (IP, name, password) and they are OK, so it is not a problem with the authentication.
Can you help me, please?
I can't understand why it is not working.
P.S. I'm not forced to use mysql++, I can use any other library without problems.
ASKER
Thank you for your support.
I forgot to mention that I'm using Ubuntu.
I can use any method to connect to the database even if I would prefer a simple solution since I'm just a beginner.
I was looking at you link, is there any example that I can test to check if it works?
I forgot to mention that I'm using Ubuntu.
I can use any method to connect to the database even if I would prefer a simple solution since I'm just a beginner.
I was looking at you link, is there any example that I can test to check if it works?
I use MySQL Workbench to check connections to make sure they are working. It is a complete standalone program written by MySQL. If it doesn't work thru MySQL Workbench, then it's not going to work in your code.
ASKER
I solved it by moving the database to my local server.
Thank you for your efforts.
Thank you for your efforts.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And there are 'official' connectors for most OSs also. https://dev.mysql.com/downloads/connector/cpp/