Neto
Neto

Reputation: 113

C API : Using Variables In mysql query

Im trying to call my str date variable in this function:

int main(int argc, char **argv)
{ 
  int status = 0;   
  char query[300], data[15], hora[10];
  strDataHora(data, hora);
  printf("Date: %s\n", data);
  MYSQL *con = mysql_init(NULL);  

  if (con == NULL)
  {
      fprintf(stderr, "mysql_init() failed\n");
      exit(1);
  }  

 if (mysql_real_connect(con, "localhost", "root", "ufmg3duc", 
         "stream_net", 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL) 
  {
      finish_with_error(con);
  }    

 if (mysql_query(con, "SELECT Id FROM audiencia WHERE data LIKE ('%s') AND (hora LIKE ('%:00:%') OR hora LIKE ('%:30:%')) AND hora > '03:01'",data))

  {
      finish_with_error(con);
  }

But I dont know how to call 'date' in mysql_query:

 if (mysql_query(con, "SELECT Id FROM audiencia WHERE data LIKE ('%s') AND (hora LIKE ('%:00:%') OR hora LIKE ('%:30:%')) AND hora > '03:01'",data))

How can i select Id when my actual date is a str variable type?

Upvotes: 0

Views: 3066

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

You have to store this query in a variable (eg. with sprintf or snprintf) and escape all those %using %%

char query[300];
...
sprintf(query, "SELECT Id FROM audiencia WHERE data LIKE ('%s') ...", data);
mysql_query(con, query);

Upvotes: 1

Related Questions