C function to get data from a MySQL Database and store as a variable -


i able c function insert , update cell on table, having issues able value cell , have saved variable can use compare , calculations on it. thank in advance.

void mia_get_data_temperature() {      mysql_res *query_results = mysql_store_result(conn);     mysql_row row; //this declare row variable       //int total_rows = mysql_num_rows(query_results);     int num_fields = mysql_num_fields(query_results);     int i;     char buffer[256]; // setting buffer query string       const char *query = "select temperature `temperature` mode='current_temperature'";        //snprintf() - safer thatn sprint use     //checking make sure query string not large buffer & formatting query passed     if (snprintf(buffer, sizeof(buffer), query) >= sizeof(buffer))     {         printf("issue buffer \n");         exit (-1);      }        //reading mysql table      if(mysql_query(conn, buffer) !=0)     {         fprintf(stderr, "%s\n", mysql_error(conn));         exit (-1);     } else {          while((row = mysql_fetch_row(query_results)) !=0)         {             (i = 0; < num_fields; i++)             {                 ??? saving result variable              }          }     }        } 

one of obvious problems code you're doing things in wrong order -- need run select query before grab result set, should calling mysql_query() before call mysql_store_result() , mysql_num_fields() instead of after.

also, although won't hurt anything, don't need snprintf() query string buffer here because aren't writing formatted values it.

as saving values query, they're returned text, if want else, float or int, you'll need convert accordingly.

so if adjust code bit, skip unnecessary snprintf(), , trivial results (assuming temperature float value), might get:

void mia_get_data_temperature() {   const char *query = "select temperature `temperature` "                       "mode='current_temperature'";    if (mysql_query(conn, query) != 0)   {     fprintf(stderr, "%s\n", mysql_error(conn));     exit(-1);   } else {     mysql_res *query_results = mysql_store_result(conn);     if (query_results) { // make sure there *are* results..       mysql_row row;        while((row = mysql_fetch_row(query_results)) !=0)       {         /* since query requests 1 column, i'm          * using 'row[0]' first field. */          /* set float 'f' value in 'row[0]', or          * 0.0f if it's null */         float f = row[0] ? atof(row[0]) : 0.0f;          /* whatever need 'f' */         printf("%f\n", f);       }        /* free results when done */       mysql_free_result(query_results);     }   } } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -