sqlite3-实现C++类封装

sqlite3-实现C++类封装,第1张

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

、前言:

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

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

二、类源码:

1.头文件:sqliteWrapper.h

@H_403_27@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 rene.nyffenegger@adp-gmbh.ch
25
*/
@H_403_27@26
@H_403_27@27 #ifndef sqlITE_WRAPPER_H__
@H_403_27@28#define sqlITE_WRAPPER_H__
@H_403_27@29
@H_403_27@30 #include <string>
@H_403_27@31 #include <vector>
@H_403_27@32
@H_403_27@33 #include "sqlite3.h"
@H_403_27@34
@H_403_27@35class sqliteStatement{
@H_403_27@36private:
@H_403_27@37// sqliteStatement's ctor only to be called by sqliteWrapper
@H_403_27@38 frIEnd class sqliteWrapper;
@H_403_27@39 sqliteStatement(std::stringconst& statement,sqlite3* db);
@H_403_27@40
@H_403_27@41public:
@H_403_27@42 sqliteStatement();
@H_403_27@43
@H_403_27@44enum dataType{
@H_403_27@45 INT=sqlITE_INTEGER,
@H_403_27@46 FLT=sqlITE_float,128); line-height:1.5!important">47 TXT=sqlITE_TEXT,128); line-height:1.5!important">48 BLB=sqlITE_BLOB,128); line-height:1.5!important">49 Nul=sqlITE_NulL,128); line-height:1.5!important">50 };
@H_403_27@51
@H_403_27@52 dataType DataType(int pos_zero_indexed);
@H_403_27@53
@H_403_27@54int ValueInt(55 std::string ValueString(56
@H_403_27@57 sqliteStatement(const sqliteStatement&);58~sqliteStatement();
@H_403_27@59
@H_403_27@60sqliteStatement& operator=(sqliteStatement const&);61
@H_403_27@62bool Bind(int pos_zero_indexed,std::const& value);
@H_403_27@63double value);
@H_403_27@64int value);
@H_403_27@65bool BindNull(66
@H_403_27@67bool Execute();
@H_403_27@68
@H_403_27@69bool NextRow();
@H_403_27@70
@H_403_27@71 Call reset if not depending on the NextRow cleaning up.
72 For example for select count(*) statements
73bool reset();
@H_403_27@74
@H_403_27@75bool RestartSelect();
@H_403_27@76
@H_403_27@7778voID DecreaseRefCounter();79
@H_403_27@80int* ref_counter_; not yet used...81 sqlite3_stmt* stmt_;
@H_403_27@82 };
@H_403_27@83
@H_403_27@84class sqliteWrapper {
@H_403_27@8586 sqliteWrapper();
@H_403_27@87
@H_403_27@88bool Open(std::const& db_file);
@H_403_27@89
@H_403_27@90class ResultRecord {
@H_403_27@9192 std::vector<std::string> fIElds_;
@H_403_27@93 };
@H_403_27@94
@H_403_27@95class Resulttable {
@H_403_27@96 frIEnd 9798 Resulttable():ptr_cur_record_(@H_403_27@0){}
@H_403_27@99
@H_403_27@100 std::vector<ResultRecord> records_;
@H_403_27@101
@H_403_27@102 ResultRecord* next(){
@H_403_27@103if (ptr_cur_record_<records_.size()){
@H_403_27@104return&(records_[ptr_cur_record_++]);
@H_403_27@105 }
@H_403_27@106return@H_403_27@0;
@H_403_27@107 }
@H_403_27@108
@H_403_27@109110voID reset(){
@H_403_27@111 records_.clear();
@H_403_27@112 ptr_cur_record_=@H_403_27@113 }
@H_403_27@114
@H_403_27@115116 unsigned int ptr_cur_record_;
@H_403_27@117 };
@H_403_27@118
@H_403_27@119bool SelectStmt(std::const& stmt,Resulttable& res);
@H_403_27@120bool DirectStatement(std::const& stmt);
@H_403_27@121 sqliteStatement* Statement(std::122
@H_403_27@123 std::string LastError();
@H_403_27@124
@H_403_27@125 Transaction related126bool Begin();
@H_403_27@127bool Commit();
@H_403_27@128bool Rollback();
@H_403_27@129
@H_403_27@130131
@H_403_27@132staticint SelectCallback(voID*p_data,255); line-height:1.5!important">int num_fIElds,255); line-height:1.5!important">char**p_fIElds,255); line-height:1.5!important">char**p_col_names);
@H_403_27@133
@H_403_27@134 sqlite3* db_;
@H_403_27@135 };
@H_403_27@136
@H_403_27@137#endif

2.类实现文件:sqliteWrapper.cpp


2 sqliteWrapper.cpp
3
4 copyright (C) 2004 René Nyffenegger
5
6 This source code is provIDed 'as-is',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 rene.nyffenegger@adp-gmbh.ch
25
26
27
@H_403_27@28 #include sqliteWrapper.h@H_403_27@29 Todo: raus30 #include <windows.h>
@H_403_27@31
@H_403_27@32 sqliteWrapper::sqliteWrapper():db_(@H_403_27@0){
@H_403_27@33 }
bool sqliteWrapper::Open(std::const& db_file){
if(sqlite3_open(db_file.c_str(),&db_)!=sqlITE_OK){
returnfalse;
@H_403_27@38 }
@H_403_27@39true;
@H_403_27@40 }
@H_403_27@41
@H_403_27@42bool sqliteWrapper::SelectStmt(std::& res){
@H_403_27@43char*errmsg;
int ret;
@H_403_27@45
@H_403_27@46 res.reset();
@H_403_27@47
@H_403_27@48 ret=sqlite3_exec(db_,stmt.c_str(),SelectCallback,static_cast<voID*>(&res),&errmsg);
@H_403_27@49
@H_403_27@50if (ret!=sqlITE_OK){
@H_403_27@5152 }
@H_403_27@53 if (ret != sqlITE_OK) {55 std::cout << stmt << " [" << errmsg << "]" << std::endl;56 }57 }
@H_403_27@58
@H_403_27@59 Todo parameter p_col_namesint sqliteWrapper::SelectCallback(char** p_col_names) {
@H_403_27@61 Resulttable* res=reinterpret_cast<Resulttable*>(p_data);
@H_403_27@62
@H_403_27@63 ResultRecord record;
@H_403_27@64
@H_403_27@65 #ifdef sqlITE_WRAPPER_REPORT_ColUMN_nameS
@H_403_27@66 Hubert Castelain: column names in the first row of res if res is empty67
@H_403_27@68if(res->records_.size()==@H_403_27@0) {
@H_403_27@69 ResultRecord col_names;
for(int i=@H_403_27@0;i<num_fIElds;i++){
@H_403_27@72if(p_fIElds[i])
@H_403_27@73 col_names.fIElds_.push_back(p_col_names[i]);
@H_403_27@74else
@H_403_27@75 col_names.fIElds_.push_back((null)"); or what else ?76 }
@H_403_27@77 res->records_.push_back(col_names);
@H_403_27@78 }
@H_403_27@79#endif
@H_403_27@80
@H_403_27@810;i<num_fIElds;i++) {
@H_403_27@82 Hubert Castelain: special treatment if null8384 record.fIElds_.push_back(p_fIElds[i]);
@H_403_27@86 record.fIElds_.push_back(<null>");
@H_403_27@87 }
@H_403_27@88
@H_403_27@89 res->records_.push_back(record);
@H_403_27@90
@H_403_27@92 }
@H_403_27@93
@H_403_27@94 sqliteStatement* sqliteWrapper::Statement(std::const& statement){
@H_403_27@95 sqliteStatement* stmt;
@H_403_27@96try{
@H_403_27@97 stmt=new sqliteStatement(statement,db_);
@H_403_27@98return stmt;
@H_403_27@99 }
@H_403_27@100catch(constchar* e){
@H_403_27@101102 }
@H_403_27@103 }
@H_403_27@104
@H_403_27@105 sqliteStatement::sqliteStatement(std::* db) {
if(sqlite3_prepare(
@H_403_27@107 db,128); line-height:1.5!important">108 statement.c_str(),0); line-height:1.5!important"> stmt109-@H_403_27@1,0); line-height:1.5!important">读取的字节长度,若小于0,则终止;若大于0,则为能读取的最大字节数110&stmt_,0); line-height:1.5!important">用来指向输入参数中下一个需要编译的SQL语句存放的sqlite statement对象的指针111@H_403_27@0指针:指向stmt未编译的部分112 )
@H_403_27@113!=sqlITE_OK){
@H_403_27@114throw sqlite3_errmsg(db);
@H_403_27@115 }
@H_403_27@116
@H_403_27@117if(!stmt_){
@H_403_27@118throwstmt_ is 0";
@H_403_27@119 }
@H_403_27@120 }
@H_403_27@121
@H_403_27@122 sqliteStatement::~sqliteStatement(){
@H_403_27@123
语法: int sqlite3_finalize(sqlite3_stmt *pStmt);if(stmt_) sqlite3_finalize(stmt_);
@H_403_27@127 }
@H_403_27@128
@H_403_27@129 sqliteStatement::sqliteStatement():
@H_403_27@130 stmt_(@H_403_27@0)
@H_403_27@131 {
@H_403_27@132 }
@H_403_27@134bool sqliteStatement::Bind(const& value){
@H_403_27@135if (sqlite3_bind_text(
@H_403_27@136 stmt_,128); line-height:1.5!important">137 pos_zero_indexed+ 通配符索引138 value.c_str(),128); line-height:1.5!important">139 value.length(),0); line-height:1.5!important"> 文本长度140 sqlITE_TRANSIENT sqlITE_TRANSIENT: sqlite 自我复制141 )
@H_403_27@142!=sqlITE_OK) {
@H_403_27@143144 }
@H_403_27@145146 }
@H_403_27@147
@H_403_27@148double value) {
@H_403_27@149if(sqlite3_bind_double(
@H_403_27@150 stmt_,128); line-height:1.5!important">151 pos_zero_indexed+@H_403_27@152 value
@H_403_27@153 )
@H_403_27@154!=sqlITE_OK) {
@H_403_27@155156 }
@H_403_27@157158 }
@H_403_27@159
@H_403_27@160int value) {
@H_403_27@161if (sqlite3_bind_int(
@H_403_27@162 stmt_,128); line-height:1.5!important">163 pos_zero_indexed+@H_403_27@164 value
@H_403_27@165 )
@H_403_27@166!=sqlITE_OK){
@H_403_27@167168 }
@H_403_27@169170 }
@H_403_27@171
@H_403_27@172bool sqliteStatement::BindNull(int pos_zero_indexed){
@H_403_27@173if (sqlite3_bind_null(
@H_403_27@174 stmt_,128); line-height:1.5!important">175 pos_zero_indexed+@H_403_27@176 )
@H_403_27@177!=sqlITE_OK) {
@H_403_27@178179 }
@H_403_27@180181 }
@H_403_27@182
@H_403_27@183bool sqliteStatement::Execute() {
@H_403_27@184int rc=sqlite3_step(stmt_);
@H_403_27@185if (rc==sqlITE_BUSY){
@H_403_27@186 ::MessageBox(@H_403_27@0,sqlITE_BUSY",128); line-height:1.5!important">0);
@H_403_27@187188 }
@H_403_27@189if(rc==sqlITE_ERROR){
@H_403_27@190 ::MessageBox(sqlITE_ERROR@H_403_27@191192 }
@H_403_27@193if(rc==sqlITE_MISUSE){
@H_403_27@194 ::MessageBox(@H_403_27@195196 }
@H_403_27@197if(rc!=sqlITE_DONE){
@H_403_27@198sqlite3_reset(stmt_);199200 }
@H_403_27@201 sqlite3_reset(stmt_);
@H_403_27@202203 }
@H_403_27@204
@H_403_27@205 sqliteStatement::dataType sqliteStatement::DataType(206return dataType(sqlite3_column_type(stmt_,pos_zero_indexed));
@H_403_27@207 }
@H_403_27@208
@H_403_27@209int sqliteStatement::ValueInt(210return sqlite3_column_int(stmt_,pos_zero_indexed);
@H_403_27@211 }
@H_403_27@212
@H_403_27@213 std::string sqliteStatement::ValueString(214return std::string(reinterpret_cast<char*>(sqlite3_column_text(stmt_,pos_zero_indexed)));
@H_403_27@215 }
@H_403_27@216
@H_403_27@217bool sqliteStatement::RestartSelect(){
@H_403_27@218 sqlite3_reset(stmt_);
@H_403_27@219220 }
@H_403_27@221
@H_403_27@222bool sqliteStatement::reset(){
@H_403_27@223224
@H_403_27@225 sqlite3_reset(stmt_);
@H_403_27@226
@H_403_27@227if (rc==sqlITE_ROW) 228229 }
@H_403_27@230
@H_403_27@231bool sqliteStatement::NextRow() {
@H_403_27@232233
@H_403_27@234if (rc==sqlITE_ROW){
@H_403_27@235236 }
@H_403_27@237if(rc==sqlITE_DONE){
@H_403_27@238 sqlite3_reset(stmt_);
@H_403_27@239240 }
@H_403_27@241else242 ::MessageBox(sqliteStatement::NextRow sqlITE_MISUSE@H_403_27@243 }
@H_403_27@244if(rc==sqlITE_BUSY){
@H_403_27@245 ::MessageBox(sqliteStatement::NextRow sqlITE_BUSY@H_403_27@246 }
@H_403_27@247248 ::MessageBox(sqliteStatement::NextRow sqlITE_ERROR@H_403_27@249 }
@H_403_27@250251 }
@H_403_27@252
@H_403_27@253bool sqliteWrapper::DirectStatement(std::const& stmt){
@H_403_27@254255256
@H_403_27@257 ret=sqlite3_exec(db_,128); line-height:1.5!important">258
@H_403_27@259if(ret!=sqlITE_OK) {
@H_403_27@260261 }
@H_403_27@262263
@H_403_27@264if(ret != sqlITE_OK) {265266}267 }
@H_403_27@268
@H_403_27@269 std::string sqliteWrapper::LastError(){
@H_403_27@270return sqlite3_errmsg(db_);
@H_403_27@271 }
@H_403_27@272
@H_403_27@273bool sqliteWrapper::Begin(){
@H_403_27@274return DirectStatement(begin@H_403_27@275 }
@H_403_27@276
@H_403_27@277bool sqliteWrapper::Commit(){
@H_403_27@278commit@H_403_27@279 }
@H_403_27@280
@H_403_27@281bool sqliteWrapper::Rollback(){
@H_403_27@282rollback@H_403_27@283 } 3.实例文件:

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


2 * 功能:创建数据库和数据库表。
3 * open():若指定数据库不存在,则创建;否则打开
4 * DirectStatement():可以用于执行大部分SQL语句,但对SELECT语句有些例外。
5 *
6
7 #include <iostream>
@H_403_27@8
#include @H_403_27@9
@H_403_27@10int main() {
@H_403_27@11 sqliteWrapper sqlite;
@H_403_27@12if (sqlite.Open(sqliteWrapper.db")) {
@H_403_27@13 std::cout<<sqliteWrapper.db created or opened"<<std::endl;
@H_403_27@14 }
@H_403_27@15else {
@H_403_27@16 std::cout<<Couldn't open sqliteWrapper.db@H_403_27@17 }
@H_403_27@18
@H_403_27@19if(sqlite.DirectStatement(Create table foo(bar,baz)")){
@H_403_27@20 std::cout<<table foo created@H_403_27@21 }
@H_403_27@2223 std::cout<<Couldn't insert into foo@H_403_27@24
@H_403_27@2526 } 3.2 插入数据:Insert_DB_Data.cpp


2 * 功能:向数据库表插入记录
3 *
4
5 #include <iostream>
@H_403_27@6

@H_403_27@7 #include @H_403_27@8
@H_403_27@910 sqliteWrapper sqlite;
@H_403_27@1112 std::cout<<@H_403_27@13 }
@H_403_27@1415 std::cout<<@H_403_27@16 }
@H_403_27@17
@H_403_27@18insert into foo values(1,2)@H_403_27@19 std::cout<<values(1,2) into foo inserted@H_403_27@20
3.3 绑定参数执行:Bind_Param_Execute.cpp


2 * 功能:Bind()封装sqlite3_bind_*系列函数,类中表现为重载函数,给sql声明中的通配符赋值,若未绑定,则为空
3 * Execute():实现sqlite3_step(s) 和sqlite3_reset(s)机制。
4
int main(){
if(sqlite.Open(else{
@H_403_27@18
sqliteStatement* stmt=sqlite.Statement(insert into foo values(?,?)@H_403_27@19
@H_403_27@20if(stmt->Bind(@H_403_27@3)){
@H_403_27@21 std::cout<<value 3 successfully bound at pos 0@H_403_27@22 }
@H_403_27@2324 std::cout<<value 3 NOT successfully bound at pos 0: "<<sqlite.LastError()<<std::endl;
@H_403_27@25 }
@H_403_27@264)){
@H_403_27@27 std::cout<<value 4 successfully bound at pos 1@H_403_27@28 }
@H_403_27@30 std::cout<<value 4 NOT successfully bound at pos 1:@H_403_27@31 }
@H_403_27@33 第一次执行Execute34if(stmt->Execute()){
@H_403_27@35 std::cout<<statement executed@H_403_27@36 }
@H_403_27@38 std::cout<<error executing statement: @H_403_27@39 }
@H_403_27@5)){
@H_403_27@42 std::cout<<value 5 successfully bound at pos 0@H_403_27@43 }
@H_403_27@45 std::cout<<value 5 NOT successfully bound at pos 0@H_403_27@46 }
@H_403_27@486)){
@H_403_27@49 std::cout<<value 6 successfully bound at pos 1@H_403_27@50 }
@H_403_27@52 std::cout<<value 6 NOT successfully bound at pos 1@H_403_27@53 }
@H_403_27@54
第二次执行Execute57 std::cout<<@H_403_27@58 }
@H_403_27@60 std::cout<<@H_403_27@61 }
@H_403_27@64 } 3.4 输出数据库数据:Print_DB_Data.cpp


2 * 功能:演示了使用类成员函数获取数据库表的数据,并将数据输出到屏幕
3 * Statement():返回一个指向sqliteStatement类的指针
4 * NextRow():只要数据未取完,就返回True
5 * DataType():返回访问列的数据类型
6 * ValueString():返回std::string.
7
8 #include <iostream>
@H_403_27@9
#include @H_403_27@10
@H_403_27@12 sqliteWrapper sqlite;
@H_403_27@1314 std::cout<<@H_403_27@15 }
@H_403_27@1617 std::cout<<@H_403_27@18 }
@H_403_27@20 sqliteStatement* stmt=sqlite.Statement(select * from foo@H_403_27@21
while(stmt->NextRow()){
@H_403_27@23 std::cout<<stmt->DataType (@H_403_27@0)<< - "<<stmt->DataType (@H_403_27@1) << | "<<
@H_403_27@24 stmt->ValueString("<<stmt->ValueString(@H_403_27@1)<<std::endl;
@H_403_27@2728 } 总结

以上是内存溢出为你收集整理的sqlite3-实现C++类封装全部内容,希望文章能够帮你解决sqlite3-实现C++类封装所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1172091.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存