Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question