forked from EliasOenal/multimon-ng
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqlite.c
52 lines (47 loc) · 1.49 KB
/
sqlite.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
49
50
51
52
#include "multimon.h"
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <sqlite3.h>
//TODO Error handling
int store_message (int sql_address, int sql_function, char *sql_message, char *sql_baud)
{
int error = 0;
int timestamp;
sqlite3 *message_db;
const char create_table[] = "CREATE TABLE IF NOT EXISTS messages ("
"address INTEGER, "
"function INTEGER, "
"message TEXT, "
"baud TEXT, "
"timestamp INTEGER"
")";
const char insert_message[] = "INSERT INTO messages"
"(address, function, message, baud, timestamp)"
"VALUES (?,?,?,?,?)";
sqlite3_stmt *insert_stmt = NULL;
//Open database, will create if not existing
error = sqlite3_open (pocsag_database, &message_db);
if (error)
{
printf ("There was a problem creating/opening the message database \n");
return (1);
}
//Create message table
error = sqlite3_exec (message_db, create_table, 0 ,0 ,0);
error = sqlite3_prepare_v2 (message_db, insert_message, -1, &insert_stmt, NULL);
//Read Unix time
timestamp = time(NULL);
// Bind values
error = sqlite3_bind_int (insert_stmt, 1, sql_address);
error = sqlite3_bind_int (insert_stmt, 2, sql_function);
error = sqlite3_bind_text (insert_stmt, 3, sql_message, strlen(sql_message), NULL);
error = sqlite3_bind_text (insert_stmt, 4, sql_baud, strlen(sql_baud), NULL);
error = sqlite3_bind_int64 (insert_stmt, 5, timestamp);
//Store
error = sqlite3_step (insert_stmt);
//Close DB
error = sqlite3_finalize (insert_stmt);
sqlite3_close (message_db);
return (0);
}