-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cpp
More file actions
385 lines (359 loc) · 14.1 KB
/
db.cpp
File metadata and controls
385 lines (359 loc) · 14.1 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "db_def.h"
/* Connect to dababase Filelist */
record::record(){
//QString createsql = "CREATE TABLE IF NOT EXISTS Filelist(Name text,Path text);";
//connect_db();
create_tbl();
}
record::~record(){
disconnet_db();
}
bool record::connect_db(){
int rc = sqlite3_open_v2("./filelist", &db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, 0);
//int rc = sqlite3_open("./filelist", &db);
if (rc != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("open db fail(%1,%2)").arg(rc).arg(sqlite3_errmsg(db)));
return false;
}
else{
emit kiwiLog(Q_FUNC_INFO, tr("open db ok"));
return true;
}
}
/* Disconnect the database Filelist */
void record::disconnet_db(){
if (!db)
return;
int rc = sqlite3_close(db);
if (rc != SQLITE_OK)
emit kiwiError(Q_FUNC_INFO, tr("close db fail(%1,%2)").arg(rc).arg(sqlite3_errmsg(db)));
else{
db = 0;
emit kiwiLog(Q_FUNC_INFO, tr("close db ok"));
}
}
QStringList record::read_db(){
if (!connect_db())
return QStringList(); //return empty string
int rc=0, rc1=0;
QStringList result;
sqlite3_stmt *pStmt=0;
const char *zTail;
do{
rc1 = sqlite3_prepare_v2(db, "SELECT * FROM Filelist;", -1, &pStmt, &zTail);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("sqlite3_prepare_v2 fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc = KiwiSQLitePrepareError;
break;
}
if (!pStmt){
emit kiwiError(Q_FUNC_INFO, tr("statement handle is null!"));
rc = KiwiSQLiteStatementNULL;
break;
}
do{
rc1 = sqlite3_step(pStmt);
if (rc1 == SQLITE_DONE){
//result = "(not-found)";
break;
}
if (rc1 != SQLITE_ROW){
emit kiwiError(Q_FUNC_INFO, tr("result not ready!"));
rc = KiwiSQLiteResultNotReady;
break;
}
for(int i=0;i<2;i++){
char* s=(char*)sqlite3_column_text(pStmt,i);
result.append(QString::fromUtf8(s));
}
}while(rc1 != SQLITE_DONE);
return result;
//char *s = (char*)sqlite3_column_text(pStmt, 0);
/*
if (!s){
emit kiwiError(Q_FUNC_INFO, tr("cacheFileDir is null!"));
rc = 10;
break;
}
*/
//result = QStringList(s);
}while(0);
if (pStmt)
sqlite3_finalize(pStmt);
pStmt = 0;
return result;
/*
int rows=0,cols=0;
char** result,*errMsg;
QString querysql = "SELECT * FROM Filelist;";
sqlite3_get_table(db , querysql.toLatin1(), &result , &rows, &cols, &errMsg);
return *result;
*/
}
bool record::create_tbl(){
ColumnDef_t columns;
if (!connect_db())
return false;
int rc=exec_SQL(db,"CREATE TABLE IF NOT EXISTS Filelist ( name text, path text )");
if (rc != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("open db fail(%1,%2)").arg(rc).arg(sqlite3_errmsg(db)));
return false;
}
else{
emit kiwiLog(Q_FUNC_INFO, tr("open db ok"));
return true;
}
}
//**** Update record in the database ****
bool record::update_db(QString old_name, QString new_name){
if (!connect_db())
return false;
int rc=0, rc1;
sqlite3_stmt *pStmt=0;
const char *zTail;
do{
rc1 = sqlite3_prepare_v2(db, "update Filelist set name=:new_name where name=:old_name", -1, &pStmt, &zTail);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("sqlite3_prepare_v2 fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLitePrepareError;
break;
}
if (!pStmt){
emit kiwiError(Q_FUNC_INFO, tr("statement handle is null!"));
rc=KiwiSQLiteStatementNULL;
break;
}
rc1 = sqlite3_bind_text(pStmt, 1, new_name.toUtf8().data(), -1, SQLITE_TRANSIENT);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("bind 'new_name' fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteBindFail;
break;
}
rc1 = sqlite3_bind_text(pStmt, 2, old_name.toUtf8().data(), -1, SQLITE_TRANSIENT);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("bind 'old_name' fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteBindFail;
break;
}
rc1 = sqlite3_step(pStmt);
if (rc1 != SQLITE_DONE){
emit kiwiError(Q_FUNC_INFO, tr("sqlite3_step fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteExecuteFail;
break;
}
}while(0);
if (pStmt)
sqlite3_finalize(pStmt);
pStmt = 0;
if (rc == 0)
emit kiwiLog(Q_FUNC_INFO, tr("(%1): %2 ").arg(old_name).arg(new_name));
//return (rc == 0);
}
//**** Add a record to database****
int record::add_db(QString name,QString path){
if (!connect_db())
return -1;
int rc=0, rc1;
sqlite3_stmt *pStmt=0;
const char *zTail;
do{
rc1 = sqlite3_prepare_v2(db, "insert into Filelist(name,path) values (:name, :path)", -1, &pStmt, &zTail);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("sqlite3_prepare_v2 fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLitePrepareError;
break;
}
if (!pStmt){
emit kiwiError(Q_FUNC_INFO, tr("statement handle is null!"));
rc=KiwiSQLiteStatementNULL;
break;
}
rc1 = sqlite3_bind_text(pStmt, 1, name.toUtf8().data(), -1, SQLITE_TRANSIENT);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("bind column 'name' fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteBindFail;
break;
}
rc1 = sqlite3_bind_text(pStmt, 2, path.toUtf8().data(), -1, SQLITE_TRANSIENT);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("bind column 'path' fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteBindFail;
break;
}
rc1 = sqlite3_step(pStmt);
if (rc1 != SQLITE_DONE){
emit kiwiError(Q_FUNC_INFO, tr("sqlite3_step fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteExecuteFail;
break;
}
}while(0);
if (pStmt)
sqlite3_finalize(pStmt);
pStmt = 0;
if (rc == 0)
emit kiwiLog(Q_FUNC_INFO, tr("new map(%1): %2 ").arg(name).arg(path));
return (rc == 0);
}
bool record::remove_record(QString path){
if (!connect_db())
return -1;
int rc=0, rc1;
sqlite3_stmt *pStmt=0;
const char *zTail;
do{
rc1 = sqlite3_prepare_v2(db, "delete from Filelist where path=:path", -1, &pStmt, &zTail);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("sqlite3_prepare_v2 fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLitePrepareError;
break;
}
if (!pStmt){
emit kiwiError(Q_FUNC_INFO, tr("statement handle is null!"));
rc=KiwiSQLiteStatementNULL;
break;
}
rc1 = sqlite3_bind_text(pStmt, 1, path.toLatin1().data(), -1, SQLITE_TRANSIENT);
if (rc1 != SQLITE_OK){
emit kiwiError(Q_FUNC_INFO, tr("bind column 'name' fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteBindFail;
break;
}
rc1 = sqlite3_step(pStmt);
if (rc1 != SQLITE_DONE){
emit kiwiError(Q_FUNC_INFO, tr("sqlite3_step fail!(%1,%2)").arg(rc1).arg(sqlite3_errmsg(db)));
rc=KiwiSQLiteExecuteFail;
break;
}
}while(0);
if (pStmt)
sqlite3_finalize(pStmt);
pStmt = 0;
if (rc == 0)
emit kiwiLog(Q_FUNC_INFO, tr(" record:(%1)deleted.").arg(path));
return (rc == 0);
}
void record::clear_db(){
/*
if (!connect_db())
return;
*/
int rc=0, rc1;
sqlite3_stmt *pStmt=0;
const char *zTail;
QString clearsql="DELETE FROM Filelist";
rc=exec_SQL(db,clearsql);
}
/* Execute simple SQL statements */
int exec_SQL(sqlite3 *db, QString sql)
{
int rc = 0;
sqlite3_stmt *pStmt = 0;
const char *zTail;
do{
int rc1 = sqlite3_prepare_v2(db, sql.toLatin1().data(), -1, &pStmt, &zTail);
if (rc1 != SQLITE_OK){
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_ERROR, __FUNCTION__, 0, "sqlite3_prepare_v2 fail!(%d,%s)\n", rc1, sqlite3_errmsg(db));
rc = KiwiSQLitePrepareError;
break;
}
if (!pStmt){
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_ERROR, __FUNCTION__, 0, "statement handle is null!\n");
rc = KiwiSQLiteStatementNULL;
break;
}
rc1=sqlite3_step(pStmt);
if (rc1!=SQLITE_DONE){
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_ERROR, __FUNCTION__, 0, "sqlite3_step fail!(%d,%s)\n", rc1, sqlite3_errmsg(db));
rc = KiwiSQLiteExecuteFail;
break;
}
}while(0);
if (pStmt)
sqlite3_finalize(pStmt);
//if (rc==0)
// UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_INFO, __FUNCTION__, 0, "execute ok.\n");
return rc;
}
QMap<QString, ColumnDef_t> QueryTableDefinition_SQLite(sqlite3 *db, QString tabName, int *pRet){
QMap<QString, ColumnDef_t> colList;
int rc = 0, rc1;
sqlite3_stmt *pStmt = 0;
const char *zTail;
do{
QString sql = "PRAGMA table_info(%1)";
sql = sql.arg(tabName);
rc1 = sqlite3_prepare_v2(db, sql.toLatin1().data(), -1, &pStmt, &zTail);
if (rc1 != SQLITE_OK){
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_ERROR, __FUNCTION__, 0, "sqlite3_prepare_v2 fail!(%d,%s)\n", rc1, sqlite3_errmsg(dblite));
rc = KiwiSQLitePrepareError;
break;
}
if (!pStmt){
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_ERROR, __FUNCTION__, 0, "statement handle is null!\n");
rc = KiwiSQLiteStatementNULL;
break;
}
while (1){
rc1 = sqlite3_step(pStmt);
if (rc1 == SQLITE_DONE)
break;
if (rc1 != SQLITE_ROW){
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_ERROR, __FUNCTION__, 0, "result not ready!\n");
rc = KiwiSQLiteResultNotReady;
break;
}
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_DEBUG, __FUNCTION__, 0, "colCount=%d\n", colCount);
ColumnDef_t col;
memset(&col, 0, sizeof(ColumnDef_t));
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_DEBUG, __FUNCTION__, 0, "col[0]=%d\n", sqlite3_column_int(pStmt, 0));
//col.index = sqlite3_column_int(pStmt, 0);
char *s = (char*)sqlite3_column_text(pStmt, 1);
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_DEBUG, __FUNCTION__, 0, "col[1]=%s\n", s);
if (s){
strncpy(col.name, s, 64);
col.name[63] = '\0';
}
else
col.name[0] = '\0';
s=(char*)sqlite3_column_text(pStmt, 2);
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_DEBUG, __FUNCTION__, 0, "col[2]=%s\n", s);
if (s){
//strncpy(col.dataType, (char*)sqlite3_column_text(pStmt, 2), 32);
//col.dataType[31] = '\0';
}
else
//col.dataType[0] = '\0';
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_DEBUG, __FUNCTION__, 0, "col[3]=%d\n", sqlite3_column_int(pStmt, 3));
//col.notNull = sqlite3_column_int(pStmt, 3);
s = (char*)sqlite3_column_text(pStmt, 4);
//UTIL4_MessageLog(CB_CYBERHOOD_LOGFILE, MSG_LOG_DEBUG, __FUNCTION__, 0, "col[4]=%s\n", s);
if (s){
//strncpy(col.defValue, (char*)sqlite3_column_text(pStmt, 4), 32);
//col.defValue[31] = '\0';
}
else
//col.defValue[0] = '\0';
colList.insert(col.name, col);
}
if (rc > 0)
break;
}while(0);
if (pStmt)
sqlite3_finalize(pStmt);
if (rc > 0){
if (pRet)
*pRet = rc;
}
else{
if (pRet)
*pRet = 0;
}
return colList;
}
/*
QString createsql = "CREATE TABLE Filelist("
"Name VARCHAR(32),"
"Path VARCHAR(10));";
QString insertsql = "INSERT INTO Contact VALUES(NULL, 'Fred', '09990123456');";
QString querysql = "SELECT * FROM Contact;";
*/