-
Notifications
You must be signed in to change notification settings - Fork 0
/
cDBman.cpp
156 lines (128 loc) · 3.09 KB
/
cDBman.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "cDBman.h"
cDBman::cDBman()
{
isOpenDB = false;
dbfile = NULL;
ConnectDB();
}
cDBman::~cDBman()
{
DisconnectDB();
isOpenDB = false;
dbfile = NULL;
}
bool cDBman::ConnectDB()
{
if (sqlite3_open(DB, &dbfile) == SQLITE_OK)
{
isOpenDB = true;
return true;
}
return false;
}
void cDBman::DisconnectDB()
{
if (isOpenDB == true)
{
sqlite3_close(dbfile);
}
}
UserData cDBman::getUserData(const string username)
{
sqlite3_stmt *statement;
UserData ud = { "", 0, 0, "", "" };
char *query = "select * from users_table";
if (sqlite3_prepare(dbfile, query, -1, &statement, 0) == SQLITE_OK)
{
int ctotal = sqlite3_column_count(statement);
int res = 0;
res = sqlite3_step(statement);
if (res == SQLITE_ROW)
{
char *ch = (char*)sqlite3_column_text(statement, 1);
if (ch != NULL)
ud.name = ch;
ch = (char*)sqlite3_column_text(statement, 2);
if (ch != NULL)
ud.energy = atoi(ch);
else ud.energy = 0;
ch = (char*)sqlite3_column_text(statement, 3);
if (ch != NULL)
ud.maxEnergy = atoi(ch);
else ud.maxEnergy = 0;
ch = (char*)sqlite3_column_text(statement, 4);
if (ch != NULL)
ud.time_in = ch;
ch = (char*)sqlite3_column_text(statement, 5);
if (ch != NULL)
ud.time_out = ch;
}
}
return ud;
}
RoomData cDBman::getRoomData(const string title)
{
sqlite3_stmt *statement;
RoomData rd = { "", 0, "" };
string q = "select * from rooms_table where room = '" + title + "'";
const char *query = q.c_str();
if (sqlite3_prepare(dbfile, query, -1, &statement, 0) == SQLITE_OK)
{
int ctotal = sqlite3_column_count(statement);
int res = 0;
res = sqlite3_step(statement);
if (res == SQLITE_ROW)
{
char *ch = (char*)sqlite3_column_text(statement, 1);
if (ch != NULL)
rd.title = ch;
else rd.energy = 0;
ch = (char*)sqlite3_column_text(statement, 2);
if (ch != NULL)
rd.energy = atoi(ch);
else rd.energy = 0;
ch = (char*)sqlite3_column_text(statement, 3);
if (ch != NULL)
rd.time_rec = ch;
}
}
return rd;
}
void cDBman::setUserData(const UserData data)
{
char energy[10];
_itoa_s(data.energy, energy, 10, 10);
string str = "update users_table set energy = '";
str.append(energy);
str.append("', time_in = '");
str.append(data.time_in);
str.append("', time_out = '");
str.append(data.time_out);
str.append("' where name = '");
str.append(USER_NAME);
str.append("'");
const char *query = str.c_str();
sqlite3_stmt *statement;
if (sqlite3_prepare(dbfile, query, -1, &statement, 0) == SQLITE_OK)
{
int result = sqlite3_step(statement);
sqlite3_finalize(statement);
}
}
void cDBman::setRoomData(const RoomData data)
{
if (data.energy == 0) return;
string str = "update rooms_table set ";
str.append("time_record = '");
str.append(data.time_rec);
str.append("' where room = '");
str.append(data.title);
str.append("'");
const char *query = str.c_str();
sqlite3_stmt *statement;
if (sqlite3_prepare(dbfile, query, -1, &statement, 0) == SQLITE_OK)
{
int result = sqlite3_step(statement);
sqlite3_finalize(statement);
}
}