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+

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
The Beast
  • 1,629
  • 2
  • 29
  • 42

2 Answers2

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;
}
Heavy
  • 1,861
  • 14
  • 25
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

The Beast
  • 1,629
  • 2
  • 29
  • 42
  • 2
    The real answer is that **your program needs error checking & handling**. – Lightness Races in Orbit Aug 10 '15 at 11:50
  • yes i agree , How to check for errors , using debugger ?? – The Beast Aug 10 '15 at 12:07
  • No, you write error handling into your code. Which book are you using? – Lightness Races in Orbit Aug 10 '15 at 12:07
  • i put the link that i have used in the post : https://www.raspberrypi.org/forums/viewtopic.php?t=31394&p=272288 – The Beast Aug 10 '15 at 12:41
  • the code now is working but i have found a lot of tutorials talking about MySql C++ connector :it offers some beneficts but for a sample application do i really need it ? – The Beast Aug 10 '15 at 12:43
  • i have learned it in the past from openclassrooms : it's a french site – The Beast Aug 10 '15 at 12:46
  • yes but they have a lot of books , here is the link to the book https://user.oc-static.com/pdf/11406-programmez-avec-le-langage-c.pdf , yes it include exception handling in the last chapter :p – The Beast Aug 10 '15 at 12:51
  • No no no. Not exception handling! Basic error checking in your code. If that e-book does not teach such logic, you should use a peer-reviewed recommended C++ learning book: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Lightness Races in Orbit Aug 10 '15 at 12:53
  • yes error checking is necessary especially in largest code ... , i don't like much using books and i look in the web for every problem i got , i don't know if this could be a bad habit but since i found solutions for every problem i got i am happy with that :) – The Beast Aug 10 '15 at 13:13
  • 2
    Yes, it is a very bad habit. You should learn from a book; you cannot expect to learn by asking questions like this. – Lightness Races in Orbit Aug 10 '15 at 15:18