Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "fileio.h"
- #include <my_global.h>
- #include <mysql.h>
- #include <stdio.h>
- #include <inttypes.h>
- #include <time.h>
- void read_data(char * sensor_data)
- {
- FILE *file = fopen(sensor_data,"rb");
- if (file == NULL)
- {
- fprintf(stderr, "Could not open sensor data\n");
- exit(1);
- }
- MYSQL *con = mysql_init(NULL);
- if (con == NULL)
- {
- fprintf(stderr, "mysql_init() failed\n");
- exit(1);
- }
- if (mysql_real_connect(con, "localhost", "root", "", "a13_syssoft", 0, NULL, 0) == NULL)
- {
- fprintf(stderr, "%s\n", mysql_error(con));
- mysql_close(con);
- exit(1);
- }
- uint16_t sensor_id = 0;
- double sensor_value = 0;
- time_t timestamp = 0;
- char * statement;
- while(!feof(file))
- {
- fread(&sensor_id,sizeof(uint16_t),1,file);
- fread(&sensor_value,sizeof(double),1,file);
- fread(×tamp,sizeof(time_t),1,file);
- asprintf(&statement, "INSERT INTO evert_borghgraef(sensor_id, sensor_value, timestamp) VALUES(%" PRIu16", %f, FROM_UNIXTIME(%ld))", sensor_id, sensor_value, timestamp);
- if (mysql_query(con, statement))
- {
- fprintf(stderr, "%s\n", mysql_error(con));
- mysql_close(con);
- exit(1);
- }
- free(statement);
- }
- fclose(file);
- mysql_close(con);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement