-
Notifications
You must be signed in to change notification settings - Fork 13
/
sqlite.ino
87 lines (78 loc) · 2.23 KB
/
sqlite.ino
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
76
77
78
79
80
81
82
83
84
85
86
87
int db_open(const char *filename, sqlite3 **db) {
int rc = sqlite3_open(filename, db);
if (rc) {
Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db));
return rc;
} else {
Serial.printf("Opened database successfully\n");
}
return rc;
}
const char* data = "Callback function called";
static int callback(void *data, int argc, char **argv, char **azColName) {
int i;
Serial.printf("%s: ", (const char*)data);
for (i = 0; i<argc; i++){
Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
Serial.printf("\n");
return 0;
}
char *zErrMsg = 0;
int db_exec(sqlite3 *db, const char *sql) {
Serial.println(sql);
long start = micros();
int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if (rc != SQLITE_OK) {
Serial.printf("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
Serial.printf("Operation done successfully\n");
}
Serial.print(F("Time taken:"));
Serial.println(micros()-start);
return rc;
}
int sqlite_select_answer(String IEEE)
{
String sql = "select * from devices where IEEE = '" + IEEE + "'";
rc = sqlite3_prepare_v2(db, sql.c_str(), 1000, &res, &tail);
if (rc != SQLITE_OK) {
String resp = "Failed to fetch data: ";
Serial.println(resp.c_str());
return 0;
}
rec_count = 0;
while (sqlite3_step(res) == SQLITE_ROW) {
rec_count++;
}
sqlite3_finalize(res);
return rec_count;
}
void sqlite_insertnewdev(String IEEE, String devname, String longaddr)
{
String sql = "INSERT INTO devices (IEEE,name,shortaddr) VALUES('" + IEEE + "','" + devname + "','" + longaddr + "');";
rc = db_exec(db, sql.c_str());
if (rc != SQLITE_OK) {
return;
}
}
void sqliteDeleteDevice(String IEEE)
{
//String sql = "delete from endpoints where IEEE = '" + IEEE + "'";
String sql = "delete from devices where IEEE = '" + IEEE + "'";
rc = db_exec(db, sql.c_str());
if (rc != SQLITE_OK) {
return;
}
}
/*
void sqlite_insertnewdev(String IEEE, String devname, String longaddr)
{
String sql = "INSERT INTO devices (IEEE,name,shortaddr) VALUES('" + IEEE + "','" + devname + "','" + longaddr + "');";
rc = db_exec(db, sql.c_str());
if (rc != SQLITE_OK) {
return;
}
}
*/