In C Language, store MySQL row data as variable to avoid segmentation fault with RPi/GPIO stuffs

43
January 13, 2019, at 03:10 AM

This is most-likely a stupid beginners question. For starters, here's my C code [segment]:

  while ((row = mysql_fetch_row(result)))
  {
  //printf("RecordID:%s\nValue:%s\n", row[0], row[1]);
  //printf("Name:%s\n", row[0]);
  //printf("\n");
  }
  //Row[0] is now the only row I'm pulling from the SELECT statement
  // it is simply a varchar field with one row in this example, "Bob"
  // My goal is simply to be able to change the value in the dB and
  // the next time this C program is executed, the new data will appear 
  // on my little LCD. (It's a RPi/GPIO project).
  #define MYDATA row[0];
  printf("Name:%c\n",MYDATA);
  //printf("Name:$s\n",row[0];  THIS WORKED JUST FINE...FOR THE PRINTF ONLY
  //lcdPrintf(lcdhd,%s",row[0] <-- this is whats giving me the Segmentation fault error
  //lcdPrintf(lcdhd,"%s",blah); <-- so I should close the db and use a variable. 
  //blah = row[0] <-- but C has no clue what I'm asking it to do here.
  lcdPrintf(lcdhd,"Hi there") <-- that works fine (so its ONLY a variable setting issue here)
  mysql_free_result(result);
  mysql_close(con);

There is obviously code before this. Opens the local dB, grabs data, etc. That part works fine, and in-fact the printf works fine when I set it to $s, row[0]. The problem is when I try to use this same varchar data (this data happens to be "Bob" just for testing sake) in the lcdPrintf, it throws a Segmentation fault error. I'm guessing I'm getting this error ONLY for this because this is a funtion headed out through my GPIO/raspberry pi to a tiny LCD screen. And, just so nobody asks this. Yes, all the RPi/GPIO works fine, too. Works perfect until I change this one line from a to b. What in the world am I doing wrong here? I've looked up 100 problems with setting variables in C (which is what headed me in this direction of using a DEFINE for it, which I know is silly. Please help!

READ ALSO
How to update FK linked to multiple table - Cascade on Update

How to update FK linked to multiple table - Cascade on Update

I have 3 tables which are linked to each other

56
How do I convert a JSON object into a MySQL row?

How do I convert a JSON object into a MySQL row?

I'd like to take a JSON string representing an object from a 3rd party API and insert it into a MySQL tableThe JSON object properties match the table fields 1-to-1

45
How to substantially decrease mySQL query processing time?

How to substantially decrease mySQL query processing time?

I have a trouble that my query in mySQL is being processing about 8-10 minutes before report finally appearsI have created few indexes and time has fallen till 2-3 minutes

22
How to get multiple rows/columns matched from another table in mysql

How to get multiple rows/columns matched from another table in mysql

I have a records table and a history table that adds timestamps as records flow through our systemThey look something like this

18