Working on a C project where I'm trying to insert values into a mysql database (linux debian on beaglebone black). The code works fine when I insert constants into the database but I cannot figure out how to get variables for date/time and a double number (temperature). Been at it for a week but cannot seem to figure it out so any insight would be appreciated.
All the assorted things I've tried either end up with a compile error, null in database or zeros. I think I'm close... but obviously missing something, probably in the INSERT INTO line?
char buf[LEN];
time_t curtime;
struct tm *loc_time;
curtime = time (NULL);
loc_time = localtime (&curtime);
strftime (buf, LEN, "%Y-%m-%d" " " "%X", loc_time);
fputs (buf, stdout);
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
if (mysql_real_connect(con, "localhost", "user", "pass", "TempDB", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}
if (mysql_query(con, "CREATE TABLE IF NOT EXISTS TempMeas(MeasTime DATETIME, Temp DOUBLE)"))
{
finish_with_error(con);
}
if (mysql_query(con, "INSERT INTO TempMeas(MeasTime, Temp) VALUES('%d', '%d')", buf, j))
{
finish_with_error(con);
}
mysql_close(con);
exit(0);