-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlitedb.cpp
113 lines (92 loc) · 2.54 KB
/
sqlitedb.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
#include "sqlitedb.h"
/////////////////////////////////////////////////
// CSQLiteRecord
/////////////////////////////////////////////////
//*****************************************************************************
CSQLiteRecord::CSQLiteRecord(void)
: m_pStatement(nullptr),
m_bDone(false)
{
}
//*****************************************************************************
CSQLiteRecord::~CSQLiteRecord(void)
{
Close();
}
//*****************************************************************************
void CSQLiteRecord::Close(void)
{
if(m_pStatement)
{
sqlite3_finalize(m_pStatement);
m_pStatement = nullptr;
}
m_bDone = false;
}
//*****************************************************************************
bool CSQLiteRecord::MoveNext(void)
{
bool bSuccess = false;
if(m_pStatement)
{
if(sqlite3_step(m_pStatement) == SQLITE_ROW)
bSuccess = true;
else
m_bDone = true;
}
return bSuccess;
}
/////////////////////////////////////////////////
// CSQLiteDB
/////////////////////////////////////////////////
//*****************************************************************************
CSQLiteDB::CSQLiteDB(void)
: m_pDB(nullptr)
{
}
//*****************************************************************************
CSQLiteDB::~CSQLiteDB(void)
{
Close();
}
//*****************************************************************************
bool CSQLiteDB::Open(PCSTR szFilename)
{
Close();
return (sqlite3_open(szFilename, &m_pDB) == SQLITE_OK);
}
//*****************************************************************************
void CSQLiteDB::Close(void)
{
if(m_pDB)
{
sqlite3_close(m_pDB);
m_pDB = nullptr;
}
}
//*****************************************************************************
bool CSQLiteDB::ExecuteQuery(PCSTR szQuery) const
{
bool bSuccess = false;
sqlite3_stmt *pStatement;
if(m_pDB && sqlite3_prepare_v2(m_pDB, szQuery, -1, &pStatement, 0) == SQLITE_OK)
{
int nReturn = sqlite3_step(pStatement);
if(nReturn == SQLITE_ROW || nReturn == SQLITE_DONE || nReturn == SQLITE_OK)
bSuccess = true;
sqlite3_finalize(pStatement);
}
return bSuccess;
}
//*****************************************************************************
bool CSQLiteDB::OpenQuery(PCSTR szQuery, CSQLiteRecord &record) const
{
bool bSuccess = false;
record.Close();
if(m_pDB && sqlite3_prepare_v2(m_pDB, szQuery, -1, &record.m_pStatement, 0) == SQLITE_OK)
{
record.MoveNext();
bSuccess = true;
}
return bSuccess;
}