-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_data.c
38 lines (29 loc) · 1 KB
/
insert_data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "globals.h"
#include <stdio.h>
#include <sqlite3.h>
#include <time.h>
#define DB_NAME "air_quality.db"
void generate_random_data(AirQualityData *data);
void insert_data(AirQualityData data);
void insert_data(AirQualityData data) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open(DB_NAME, &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return;
}
char sql[256];
sprintf(sql, "INSERT INTO AirQuality (sensor_id, timestamp, pm25, pm10, co, no2, o3, so2) "
"VALUES (%d, '%s', %.2f, %.2f, %.2f, %.2f, %.2f, %.2f);",
data.sensor_id, data.timestamp, data.pm25, data.pm10, data.co, data.no2, data.o3, data.so2);
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
} else {
printf("Data inserted successfully!\n");
}
sqlite3_close(db);
}