sqlite3-入门日记4-实现C++类封装

sqlite3-入门日记4-实现C++类封装

一、前言:

    今天试了下如何用C++类实现接口封装,感觉蛮好 。用于封装的类主要有两个,SQLiteStatement类和SQLiteWrapper类,是一个老外写的。我看了下源码,主要是对C接口进行了封装,好处自然不用说,可以重用。很佩服老外的技巧,在这里就引用下他们的代码供大家分享下他们的思想。

源代码链接: http://www.adp-gmbh.ch/sqlite/wrapper.html

二、类源码:

1.头文件:SQLiteWrapper.h

复制代码

1/*
2 SQLiteWrapper.h
3
4 Copyright (C) 2004 René Nyffenegger
5
6 This source code is provided 'as-is', without any express or implied
7 warranty. In no event will the author be held liable for any damages
8 arising from the use of this software.
9
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
13
14 1. The origin of this source code must not be misrepresented; you must not
15 claim that you wrote the original source code. If you use this source code
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original source code.
21
22 3. This notice may not be removed or altered from any source distribution.
23
24 René Nyffenegger [email protected]
25  */
26
27 #ifndef SQLITE_WRAPPER_H__
28#define SQLITE_WRAPPER_H__
29
30 #include 
31 #include 
32
33 #include "sqlite3.h"
34
35class SQLiteStatement{
36private:
37// SQLiteStatement's ctor only to be called by SQLiteWrapper
38   friend class SQLiteWrapper;
39 SQLiteStatement(std::stringconst& statement, sqlite3* db);
40
41public:
42 SQLiteStatement();
43
44enum dataType{
45 INT=SQLITE_INTEGER,
46 FLT=SQLITE_FLOAT,
47 TXT=SQLITE_TEXT,
48 BLB=SQLITE_BLOB,
49 NUL=SQLITE_NULL,
50 };
51
52 dataType DataType(int pos_zero_indexed);
53
54int ValueInt(int pos_zero_indexed);
55 std::string ValueString(int pos_zero_indexed);
56
57// SQLiteStatement(const SQLiteStatement&);
58~SQLiteStatement();
59
60//SQLiteStatement& operator=(SQLiteStatement const&);
61  
62bool Bind(int pos_zero_indexed, std::stringconst& value);
63bool Bind(int pos_zero_indexed, double value);
64bool Bind(int pos_zero_indexed, int value);
65bool BindNull(int pos_zero_indexed);
66
67bool Execute();
68
69bool NextRow();
70
71/* Call Reset if not depending on the NextRow cleaning up.
72 For example for select count(*) statements*/
73bool Reset();
74
75bool RestartSelect();
76
77private:
78//void DecreaseRefCounter();
79
80//int* ref_counter_; // not yet used...
81   sqlite3_stmt* stmt_;
82 };
83
84class SQLiteWrapper {
85public:
86 SQLiteWrapper();
87
88bool Open(std::stringconst& db_file);
89
90class ResultRecord {
91public:
92 std::vector fields_;
93 };
94
95class ResultTable {
96 friend class SQLiteWrapper;
97public:
98 ResultTable():ptr_cur_record_(0){}
99
100 std::vector records_;
101
102 ResultRecord* next(){
103if (ptr_cur_record_ 
  

复制代码

2.类实现文件:SQLiteWrapper.cpp

SQLiteWrapper.cpp

3.实例文件:

3.1 创建数据库和数据库表:Create_DB_Table.cpp

复制代码

1/*
2 * 功能:创建数据库和数据库表。
3 * open():若指定数据库不存在,则创建;否则打开
4 * DirectStatement():可以用于执行大部分SQL语句,但对SELECT语句有些例外。
5 *
6 */
7 #include 
8 #include "SQLiteWrapper.h"
9
10int main() {
11 SQLiteWrapper sqlite;
12if (sqlite.Open("SQLiteWrapper.db")) {
13 std::cout<<"SQLiteWrapper.db created or opened"< 
  

复制代码

3.2 插入数据:Insert_DB_Data.cpp

复制代码

1/*
2 * 功能:向数据库表插入记录
3 *
4 */
5 #include 
6
7 #include "SQLiteWrapper.h"
8
9int main() {
10 SQLiteWrapper sqlite;
11if (sqlite.Open("SQLiteWrapper.db")) {
12 std::cout<<"SQLiteWrapper.db created or opened"< 
  

复制代码

3.3 绑定参数执行:Bind_Param_Execute.cpp

复制代码

1/*
2 * 功能:Bind()封装sqlite3_bind_*系列函数,类中表现为重载函数,给SQL声明中的通配符赋值,若未绑定,则为空
3 * Execute():实现Sqlite3_step(s) 和Sqlite3_reset(s)机制。
4 */
5 #include 
6
7 #include "SQLiteWrapper.h"
8
9int main(){
10 SQLiteWrapper sqlite;
11if(sqlite.Open("SQLiteWrapper.db")){
12 std::cout<<"SQLiteWrapper.db created or opened"<Bind(0,3)){
21 std::cout<<"value 3 successfully bound at pos 0"<Bind(1, 4)){
27 std::cout<<"value 4 successfully bound at pos 1"<Execute()){
35 std::cout<<"statement executed"<Bind(0, 5)){
42 std::cout<<"value 5 successfully bound at pos 0"<Bind(1, 6)){
49 std::cout<<"value 6 successfully bound at pos 1"<Execute()){
57 std::cout<<"statement executed"< 
  

复制代码

3.4 输出数据库数据:Print_DB_Data.cpp

复制代码

1/*
2 * 功能:演示了使用类成员函数获取数据库表的数据,并将数据输出到屏幕
3 * Statement():返回一个指向SQLiteStatement类的指针
4 * NextRow():只要数据未取完,就返回True
5 * DataType():返回访问列的数据类型
6 * ValueString():返回std::string.
7 */
8 #include 
9 #include "SQLiteWrapper.h"
10
11int main(){
12 SQLiteWrapper sqlite;
13if (sqlite.Open("SQLiteWrapper.db")){
14 std::cout<<"SQLiteWrapper.db created or opened"<NextRow()){
23 std::cout<DataType (0)<<" - "<DataType (1) <<" | "<<
24 stmt->ValueString(0)<<" - "<ValueString(1)< 
  

复制代码

 三、后记:

      感叹SQLite的博大精深。

     ~~~路漫漫其修远兮,吾将上下而求索。

你可能感兴趣的:(sqlite3-入门日记4-实现C++类封装)