-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.cpp
97 lines (80 loc) · 1.65 KB
/
db.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
#include "db.h"
Db::Db(){
dbname = "data.db";
db = 0;
pazResult = nullptr;
}
Db::~Db(){
CloseDb();
}
bool Db::OpenDb()
{
int result = sqlite3_open(dbname.c_str(),&db);
if (0 != result)
{
return false;
}
return true;
}
bool Db::CloseDb()
{
int result = sqlite3_close(db);
if (0 != result)
{
return false;
}
return true;
}
bool Db::CreateTable(std::string sql){
char *errmsg;
int result = sqlite3_exec(db,sql.c_str(),nullptr,nullptr,&errmsg);
if (result != 0)
{
std::cout<<"error sql:"<<sql<<endl<<"exec error Message:"<<errmsg<<endl;
return false;
}
return true;
}
bool Db::WriteDb(std::string sql){
g_mutex.lock();
char *errmsg;
int result = sqlite3_exec(db,sql.c_str(),nullptr,nullptr,&errmsg);
g_mutex.unlock();
if (result != 0)
{
cout<<"error sql:"<<sql<<endl<<"exec error Message:"<<errmsg<<endl;
return false;
}
return true;
}
bool Db::ReadDb(std::string sql){
char *errmsg;
g_mutex.lock();
int result = sqlite3_get_table(db, sql.c_str(), &pazResult, &nRow, &nCol, &errmsg);
g_mutex.unlock();
if (result != 0)
{
cout<<"error sql:"<<sql<<endl<<"exec error Message:"<<errmsg<<endl;
return false;
}
// int nIndex = nCol;
// string strOut;
// for(int i=0;i<nRow;i++)
// {
// for(int j=0;j<nCol;j++)
// {
// strOut+=pazResult[j];
// strOut+=":";
// strOut+=pazResult[nIndex];
// strOut+="\n";
// ++nIndex;
// }
// }
// cout<<"str:"<<strOut<<endl;
return true;
}
void Db::freeTableData(){
g_mutex.lock();
sqlite3_free_table(pazResult);
g_mutex.unlock();
}