1

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+

1
  • 2
    Time to do some debugging! Commented Aug 10, 2015 at 11:19

2 Answers 2

1

You should check if row[0] and row[1] contain values (are not NULL). Try this:

while ( ( row = mysql_fetch_row(result)) != NULL ) {
  if(!row[0] || !row[1]){
    continue;
  }
  cout << row[0] << "\t" << row[1] << endl;
}
Sign up to request clarification or add additional context in comments.

Comments

0

i have used "temps" for both database and table , i have change the table name in

select * from "tempdat"

here temps is the name of the database and tempdat is the name of the table => it's working now

hope that will help someone having the same error

10 Comments

The real answer is that your program needs error checking & handling.
yes i agree , How to check for errors , using debugger ??
No, you write error handling into your code. Which book are you using?
i put the link that i have used in the post : raspberrypi.org/forums/viewtopic.php?t=31394&p=272288
Yes, it is a very bad habit. You should learn from a book; you cannot expect to learn by asking questions like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.