A
A
Azat20152016-07-01 07:31:06
MySQL
Azat2015, 2016-07-01 07:31:06

Why is it impossible to make an INSERT query to MySQL with a record of variables (C++)?

Tell me why when I try to use the function I get the error Error: can't execute INSERT-query? What's wrong with the request?
Table structure
1 id int(11)
2 time datetime
3 rad int(11)
4 temp float
5 hum float
6 pres int(6)

void insertToDb(short int r, float t, float h, short int p)
{
  MYSQL conn;
  if(!mysql_init(&conn))
  {
    fprintf(stderr, "Error: can't create MySQL-descriptor\n");
    exit(1);
  }
  if(!mysql_real_connect(&conn,
                         "localhost",
                         "user",
                         "xxxxx",
                         "my_db",
                         0,
                         NULL,
                         0))
  {
    fprintf(stderr, "Error: %s\n", mysql_error(&conn));
    exit(1);
  }
  if(mysql_query(&conn, "SET NAMES 'utf8' COLLATE 'utf8_general_ci'") != 0)
  {
    fprintf(stderr, "Error: can't set character set\n");
    exit(1);
  }
  char query[400];
  sprintf(query, "INSERT INTO my_table(rad, temp, hum, pres) VALUES ('%i','%f','%f','%i')", r, t, h, p);

if(mysql_query(&conn,query) !=0);
  {
    fprintf(stderr, "Error: can't execute INSERT-query\n");
    exit(1);
  }
  mysql_close(&conn);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WayMax, 2016-07-01
@Azat2015

sprintf(query, "INSERT INTO my_table(rad, temp, hum, pres) VALUES (%i, %f, %f, %i)", r, t, h, p);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question