-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_to_csv.c
48 lines (39 loc) · 1.24 KB
/
export_to_csv.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
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <sqlite3.h>
#define DB_NAME "air_quality.db"
#define CSV_FILE "sensor_data.csv"
void export_to_csv() {
sqlite3 *db;
sqlite3_stmt *res;
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;
}
FILE *csv_file = fopen(CSV_FILE, "w");
if (!csv_file) {
fprintf(stderr, "Cannot open CSV file for writing.\n");
return;
}
char *sql = "SELECT * FROM SensorData;";
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to execute query: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
fclose(csv_file);
return;
}
fprintf(csv_file, "ID,Sensor Name,Data,Timestamp\n");
while (sqlite3_step(res) == SQLITE_ROW) {
fprintf(csv_file, "%d,%s,%.2f,%s\n",
sqlite3_column_int(res, 0),
sqlite3_column_text(res, 1),
sqlite3_column_double(res, 2),
sqlite3_column_text(res, 3));
}
printf("Data exported to %s successfully!\n", CSV_FILE);
sqlite3_finalize(res);
sqlite3_close(db);
fclose(csv_file);
}