forked from digitaldex/piMeter_hardware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.h
75 lines (69 loc) · 2.4 KB
/
db.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using namespace std;
int createDB(const char *sql) {
const char *dbPath = "/home/pi/db.sqlite";
sqlite3 *dbPointer;
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_open(dbPath, &dbPointer);
if(rc) {
cerr << "SQL ERROR Opening Database: " << sqlite3_errmsg(dbPointer) << endl;
sqlite3_close(dbPointer);
return(1);
}
rc = sqlite3_prepare_v2(dbPointer, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
cerr << "SQL ERROR Prepare Statement: " << sqlite3_errmsg(dbPointer) << endl;
return(1);
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
cerr << "SQL ERROR Step Statement: " << sqlite3_errmsg(dbPointer) << endl;
return(1);
}
sqlite3_finalize(stmt);
sqlite3_close(dbPointer);
return 0;
}
void createDBTables() {
/* CREATE CURRENT TABLE IF NOT EXISTS */
const char *sql;
sql = "CREATE TABLE if not exists Current (ID INTEGER PRIMARY KEY, Timestamp TEXT, AIRMS real, BIRMS real, CIRMS real, ISUMRMS real, AI_PCF real, BI_PCF real, CI_PCF real)";
if(createDB(sql)) {
cerr << "SQL ERROR: Could not create Current Database" << endl;
};
/* CREATE VOLTAGE TABLE IF NOT EXISTS */
sql = "CREATE TABLE if not exists Voltage (ID INTEGER PRIMARY KEY, Timestamp TEXT, AVRMS real, BVRMS real, CVRMS real, AV_PCF real, BV_PCF real, CV_PCF real)";
if(createDB(sql)) {
cerr << "SQL ERROR: Could not create Voltage Database" << endl;
};
/* CREATE POWER TABLE IF NOT EXISTS */
sql = "CREATE TABLE if not exists Power (ID INTEGER PRIMARY KEY, Timestamp TEXT, AWATT real, AVA real, BWATT real, BVA real, CWATT real, CVA real, PWATT_ACC real, PVAR_ACC real)";
if(createDB(sql)) {
cerr << "SQL ERROR: Could not create Power Database" << endl;
};
}
int writeDB(const char *sql) {
const char *dbPath = "/home/pi/db.sqlite";
sqlite3 *dbPointer;
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_open(dbPath, &dbPointer);
if(rc) {
cerr << "SQL ERROR Opening Database: " << sqlite3_errmsg(dbPointer) << endl;
sqlite3_close(dbPointer);
return(1);
}
rc = sqlite3_prepare_v2(dbPointer, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
cerr << "SQL ERROR Prepare Statement: " << sqlite3_errmsg(dbPointer) << endl;
return(1);
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
cerr << "SQL ERROR Step Statement: " << sqlite3_errmsg(dbPointer) << endl;
return(1);
}
sqlite3_finalize(stmt);
sqlite3_close(dbPointer);
return 0;
}