i'm trying to make a c++ work with MySql i have tested the following code from the link
https://www.raspberrypi.org/forums/viewtopic.php?t=31394&p=272288
#include <iostream>
#include <mysql/mysql.h> // I added include /usr/include/mysql/ to ld.so.conf which is why that works
using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
#define HOST "localhost" // you must keep the quotes on all four items,
#define USER "root" // the function "mysql_real_connect" is looking for a char datatype,
#define PASSWD "123" // without the quotes they're just an int.
#define DB "temps"
int main()
{
//initialize database connection
mysql_init(&mysql);
// the three zeros are: Which port to connect to, which socket to connect to
// and what client flags to use. unless you're changing the defaults you only need to put 0 here
connection = mysql_real_connect(&mysql,HOST,USER,PASSWD,DB,0,0,0);
// Report error if failed to connect to database
if (connection == NULL) {
cout << mysql_error(&mysql) << endl;
return 1;
}
//Send query to database
query_state = mysql_query(connection, "select * from temps");
// store result
result = mysql_store_result(connection);
while ( ( row = mysql_fetch_row(result)) != NULL ) {
// Print result, it prints row[column_number])
cout << row[0] << "\t" << row[1] << endl;
}
return 0;
}
the file compiled successfully with :
g++ -o sqlOut -lmysqlclient sqlTest.cpp
but i got the error :
segmentation fault when trying to run the compiled file !!!
any help will be appreciated , thanks
NB : i need to save data from my c++ file into MySql database and view it with phpMyAdmin , it will be very helpfull if you give me another link aor turtorial to foollow
i'm using raspberry pi b+