一、前言:
今天试了下如何用C++类实现接口封装,感觉蛮好 。用于封装的类主要有两个,SQLiteStatement类和SQLiteWrapper类,是一个老外写的。我看了下源码,主要是对C接口进行了封装,好处自然不用说,可以重用。很佩服老外的技巧,在这里就引用下他们的代码供大家分享下他们的思想。
源代码链接: http://www.adp-gmbh.ch/sqlite/wrapper.html
二、类源码:
1.头文件:SQLiteWrapper.h
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
2.类实现文件:SQLiteWrapper.cpp
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
3.实例文件:
3.1 创建数据库和数据库表:Create_DB_Table.cpp
1/*
2 * 功能:创建数据库和数据库表。
3 * open():若指定数据库不存在,则创建;否则打开
4 * DirectStatement():可以用于执行大部分SQL语句,但对SELECT语句有些例外。
5 *
6 */
7 #include <iostream>
8 #include "SQLiteWrapper.h"
9
10int main() {
11 SQLiteWrapper sqlite;
12if (sqlite.Open("SQLiteWrapper.db")) {
13 std::cout<<"SQLiteWrapper.db created or opened"<<std::endl;
14 }
15else {
16 std::cout<<"couldn't open SQLiteWrapper.db"<<std::endl;
17 }
18
19if(sqlite.DirectStatement("Create table foo(bar,baz)")){
20 std::cout<<"table foo created"<<std::endl;
21 }
22else
23 std::cout<<"couldn't insert into foo"<<std::endl;
24
25return0;
26 }
3.2 插入数据:Insert_DB_Data.cpp
1/*
2 * 功能:向数据库表插入记录
3 *
4 */
5 #include <iostream>
6
7 #include "SQLiteWrapper.h"
8
9int main() {
10 SQLiteWrapper sqlite;
11if (sqlite.Open("SQLiteWrapper.db")) {
12 std::cout<<"SQLiteWrapper.db created or opened"<<std::endl;
13 }
14else {
15 std::cout<<"couldn't open SQLiteWrapper.db"<<std::endl;
16 }
17
18if(sqlite.DirectStatement("insert into foo values(1,2)")){
19 std::cout<<"values(1,2) into foo inserted"<<std::endl;
20
21 }
22else
23 std::cout<<"couldn't insert into foo"<<std::endl;
24
25return0;
26 }
3.3 绑定参数执行:Bind_Param_Execute.cpp
1/*
2 * 功能:Bind()封装sqlite3_bind_*系列函数,类中表现为重载函数,给SQL声明中的通配符赋值,若未绑定,则为空
3 * Execute():实现Sqlite3_step(s) 和Sqlite3_reset(s)机制。
4 */
5 #include <iostream>
6
7 #include "SQLiteWrapper.h"
8
9int main(){
10 SQLiteWrapper sqlite;
11if(sqlite.Open("SQLiteWrapper.db")){
12 std::cout<<"SQLiteWrapper.db created or opened"<<std::endl;
13 }
14else{
15 std::cout<<"couldn't open SQLiteWrapper.db"<<std::endl;
16 }
17
18 SQLiteStatement* stmt=sqlite.Statement("insert into foo values(?,?)");
19
20if(stmt->Bind(0,3)){
21 std::cout<<"value 3 successfully bound at pos 0"<<std::endl;
22 }
23else{
24 std::cout<<"value 3 NOT successfully bound at pos 0: "<<sqlite.LastError()<<std::endl;
25 }
26if(stmt->Bind(1, 4)){
27 std::cout<<"value 4 successfully bound at pos 1"<<std::endl;
28 }
29else{
30 std::cout<<"value 4 NOT successfully bound at pos 1:"<<sqlite.LastError()<<std::endl;
31 }
32
33// 第一次执行Execute
34if(stmt->Execute()){
35 std::cout<<"statement executed"<<std::endl;
36 }
37else{
38 std::cout<<"error executing statement: "<<sqlite.LastError()<<std::endl;
39 }
40
41if(stmt->Bind(0, 5)){
42 std::cout<<"value 5 successfully bound at pos 0"<<std::endl;
43 }
44else{
45 std::cout<<"value 5 NOT successfully bound at pos 0"<<std::endl;
46 }
47
48if(stmt->Bind(1, 6)){
49 std::cout<<"value 6 successfully bound at pos 1"<<std::endl;
50 }
51else{
52 std::cout<<"value 6 NOT successfully bound at pos 1"<<std::endl;
53 }
54
55// 第二次执行Execute
56if(stmt->Execute()){
57 std::cout<<"statement executed"<<std::endl;
58 }
59else {
60 std::cout<<"error executing statement: "<<sqlite.LastError()<<std::endl;
61 }
62
63return0;
64 }
3.4 输出数据库数据:Print_DB_Data.cpp
1/*
2 * 功能:演示了使用类成员函数获取数据库表的数据,并将数据输出到屏幕
3 * Statement():返回一个指向SQLiteStatement类的指针
4 * NextRow():只要数据未取完,就返回True
5 * DataType():返回访问列的数据类型
6 * ValueString():返回std::string.
7 */
8 #include <iostream>
9 #include "SQLiteWrapper.h"
10
11int main(){
12 SQLiteWrapper sqlite;
13if (sqlite.Open("SQLiteWrapper.db")){
14 std::cout<<"SQLiteWrapper.db created or opened"<<std::endl;
15 }
16else{
17 std::cout<<"couldn't open SQLiteWrapper.db"<<std::endl;
18 }
19
20 SQLiteStatement* stmt=sqlite.Statement("select * from foo");
21
22while(stmt->NextRow()){
23 std::cout<<stmt->DataType (0)<<" - "<<stmt->DataType (1) <<" | "<<
24 stmt->ValueString(0)<<" - "<<stmt->ValueString(1)<<std::endl;
25 }
26
27return0;
28 }
三、后记:
感叹SQLite的博大精深。
~~~路漫漫其修远兮,吾将上下而求索。