Advertisement
Guest User

Untitled

a guest
May 24th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include "fileio.h"
  2. #include <my_global.h>
  3. #include <mysql.h>
  4. #include <stdio.h>
  5. #include <inttypes.h>
  6. #include <time.h>
  7.  
  8. void read_data(char * sensor_data)
  9. {
  10.   FILE *file = fopen(sensor_data,"rb");
  11.  
  12.   if (file == NULL)
  13.   {
  14.       fprintf(stderr, "Could not open sensor data\n");    
  15.       exit(1);
  16.   }
  17.  
  18.   MYSQL *con = mysql_init(NULL);
  19.  
  20.   if (con == NULL)
  21.   {
  22.       fprintf(stderr, "mysql_init() failed\n");
  23.       exit(1);
  24.   }  
  25.  
  26.   if (mysql_real_connect(con, "localhost", "root", "", "a13_syssoft", 0, NULL, 0) == NULL)
  27.   {
  28.       fprintf(stderr, "%s\n", mysql_error(con));
  29.       mysql_close(con);
  30.       exit(1);
  31.   }
  32.  
  33.   uint16_t sensor_id = 0;
  34.   double sensor_value = 0;
  35.   time_t timestamp = 0;
  36.  
  37.   char * statement;
  38.  
  39.   while(!feof(file))
  40.   {
  41.     fread(&sensor_id,sizeof(uint16_t),1,file);
  42.     fread(&sensor_value,sizeof(double),1,file);
  43.     fread(&timestamp,sizeof(time_t),1,file);
  44.    
  45.     asprintf(&statement, "INSERT INTO evert_borghgraef(sensor_id, sensor_value, timestamp) VALUES(%" PRIu16", %f, FROM_UNIXTIME(%ld))", sensor_id, sensor_value, timestamp);
  46.    
  47.     if (mysql_query(con, statement))
  48.     {  
  49.       fprintf(stderr, "%s\n", mysql_error(con));
  50.       mysql_close(con);
  51.       exit(1);
  52.     }
  53.    
  54.     free(statement);
  55.   }
  56.  
  57.   fclose(file);
  58.   mysql_close(con);
  59.   exit(0);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement