0

I created this table (results) in my sql database (test)

CREATE DATABASE `test`; 
USE `test`;
CREATE TABLE `results` (
`number` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`temp` float NOT NULL,
`fech` datetime NOT NULL,
PRIMARY KEY (`number`),
UNIQUE KEY `indice_UNIQUE` (`number`)
) ENGINE=InnoDB AUTO_INCREMENT=100;

I have the next code which reads a buffer and gets some tokens with strtok

int learn_port2(int fd)
{
 MYSQL *conn;
 MYSQL_RES *res;
 MYSQL_RES *res1;
 MYSQL_ROW row;
 char *server = "127.0.0.1";
 char *user = "root";
 char *password = "***";  // got tot keep my data secret
 char *database = "test";

 conn = mysql_init(NULL);
 if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
 {
  fprintf(stderr, "%s\n", mysql_error(conn));
  return -1;
  //finish_with_error(conn);
 }
 int n, i;
 char buff[300];
 memset(buff, 0, sizeof(buff));
 for (int x = 0; x<3; x++)
  {
   char temp[7] = "";
   char* ptr;
   int i;
   float temp;
   n=read(fd,buff,sizeof(buff));
   sleep(1);
   printf("%s", buff);
   printf("\n");

   if (buff[37] == 'N' || buff[38] == 'N' || buff[39] == 'N' || buff[40] == 'N' )
   {
    ptr = strtok(buff, ",=T()");
    i = 0;

    while (ptr != NULL)
    {
          if (i == 10)
         strcat(temp, ptr); // copies T
         ptr = strtok(NULL, ",=T()");
     i++;
    }

   printf("Results: %s\n", temp);
  }


char query[]="INSERT INTO results(`temp`,`fech`) VALUES(temp,CURRENT_TIMESTAMP)";


if(mysql_query(conn, query))
{
 fprintf(stderr, "%s\n", mysql_error(conn));
 return -1;
}

res = mysql_use_result(conn);
}

}

I have results like

Results:22.34
Results:34.56

and I want to save this results in my database. But I always have zeros in the temp column and not these results. Where is the problem in my query? Something goes wrong with the declaration of char temp[7] = ""; I think. in the query.

1
  • I found the solution with the usage of sprintf Commented Jun 6, 2013 at 8:13

1 Answer 1

1

In the query, you're trying to insert the constant string "temp" into the column. How would the MySQL library be supposed to know about variable names in your program?

Sign up to request clarification or add additional context in comments.

1 Comment

ok you are right! This is why I posted this question. How can I insert the value of the temp in the table? What can I write in my query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.