-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (118 loc) · 3.73 KB
/
Copy pathmain.cpp
File metadata and controls
176 lines (118 loc) · 3.73 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
//
// main.cpp
// sqlite3
//
// Created by Luwei on 15/1/9.
// Copyright (c) 2015年 Luwei. All rights reserved.
//
#include <iostream>
#include "sqlite3-secure/sqlite3.h"
void db_open(sqlite3 **ppDb, const std::string &path);
void db_close(sqlite3 *pDb);
void db_encrypt(sqlite3 *pDb, const std::string &password);
// DEMO
void db_createtable(sqlite3 *pDb);
void db_insert(sqlite3 *pDb);
void db_delete(sqlite3 *pDb);
void db_update(sqlite3 *pDb);
void db_select(sqlite3 *pDb);
int main()
{
std::string path = "/Users/etime/Documents/db";
std::string password = "hello,world";
sqlite3 *pDb = nullptr;
try {
db_open(&pDb, path);
db_encrypt(pDb, password);
db_createtable(pDb);
db_insert(pDb);
db_delete(pDb);
db_update(pDb);
db_select(pDb);
db_close(pDb);
}
catch (const char *what) {
printf("[DB Error]: %s\n", what);
sqlite3_close(pDb);
return -1;
}
return 0;
}
void db_open(sqlite3 **ppDb, const std::string &path) {
int c = SQLITE_OK;
if (path.empty())
c = sqlite3_open(":memory", ppDb);
else
c = sqlite3_open(path.c_str(), ppDb);
if (c != SQLITE_OK)
throw sqlite3_errmsg(*ppDb);
}
void db_close(sqlite3 *pDb) {
int c = sqlite3_close(pDb);
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
}
void db_encrypt(sqlite3 *pDb, const std::string &password) {
int c = SQLITE_OK;
if (password.empty())
return;
else
c = sqlite3_key(pDb, password.c_str(), (int)password.length());
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
// sqlite3_rekey()
}
void db_createtable(sqlite3 *pDb) {
const char *sql = "CREATE TABLE IF NOT EXISTS user"
"([id] INTEGER PRIMARY KEY, name TEXT)";
int c = sqlite3_exec(pDb, sql, nullptr, nullptr, nullptr);
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
}
void db_insert(sqlite3 *pDb) {
const char *sql = "INSERT INTO user values(NULL, 'luweimy')";
int c = sqlite3_exec(pDb, sql, nullptr, nullptr, nullptr);
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
int count = sqlite3_changes(pDb);
printf("[DB Log]: <INSERT> %d item changes\n", count);
}
void db_delete(sqlite3 *pDb) {
const char *sql = "DELETE FROM user WHERE id=2";
int c = sqlite3_exec(pDb, sql, nullptr, nullptr, nullptr);
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
int count = sqlite3_changes(pDb);
printf("[DB Log]: <DELETE> %d item changes\n", count);
}
void db_update(sqlite3 *pDb) {
const char *sql = "UPDATE user SET name=\"**luweimy**\" WHERE id=1";
int c = sqlite3_exec(pDb, sql, nullptr, nullptr, nullptr);
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
int count = sqlite3_changes(pDb);
printf("[DB Log]: <UPADTE> %d item changes\n", count);
}
void db_select(sqlite3 *pDb) {
const char *sql = "SELECT * FROM user";
sqlite3_stmt *pStmt = nullptr;
int c = sqlite3_prepare_v2(pDb, sql, (int)strlen(sql), &pStmt, nullptr);
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
c = sqlite3_step(pStmt);
if (c != SQLITE_ROW && c != SQLITE_DONE)
throw sqlite3_errmsg(pDb);
int colnum = sqlite3_column_count(pStmt);
while (c == SQLITE_ROW) {
for (int i = 0; i < colnum; i++) {
printf("%s \t \t", sqlite3_column_text(pStmt, i));
}
printf("\n");
c = sqlite3_step(pStmt);
}
if (c != SQLITE_DONE)
throw sqlite3_errmsg(pDb);
c = sqlite3_finalize(pStmt);
if (c != SQLITE_OK)
throw sqlite3_errmsg(pDb);
}