源代码链接: http://www.adp-gmbh.ch/sqlite/wrapper.HTML
国内博客:http://www.cnblogs.com/ballwql/archive/2011/03/28/1997494.HTML
/* sqliteWrapper.h copyright (C) 2004 René Nyffenegger This source code is provIDed 'as-is',without any express or implIEd warranty. In no event will the author be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose,including commercial applications,and to alter it and redistribute it freely,subject to the following restrictions: 1. The origin of this source code must not be misrepresented; you must not claim that you wrote the original source code. If you use this source code in a product,an ackNowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such,and must not be misrepresented as being the original source code. 3. This notice may not be removed or altered from any source distribution. René Nyffenegger rene.nyffenegger@adp-gmbh.ch*/#ifndef sqlITE_WRAPPER_H__#define sqlITE_WRAPPER_H__#include <string>#include <vector>#include "sqlite3.h"class sqliteStatement { private: // sqliteStatement's ctor only to be called by sqliteWrapper frIEnd class sqliteWrapper; sqliteStatement(std::string const& statement,sqlite3* db); public: sqliteStatement(); enum dataType { INT = sqlITE_INTEGER,FLT = sqlITE_float,TXT = sqlITE_TEXT,BLB = sqlITE_BLOB,Nul = sqlITE_NulL,}; dataType DataType(int pos_zero_indexed); int ValueInt (int pos_zero_indexed); std::string ValueString(int pos_zero_indexed);// sqliteStatement(const sqliteStatement&); ~sqliteStatement(); //sqliteStatement& operator=(sqliteStatement const&); bool Bind (int pos_zero_indexed,std::string const& value); bool Bind (int pos_zero_indexed,double value); bool Bind (int pos_zero_indexed,int value); bool BindNull(int pos_zero_indexed); bool Execute(); bool NextRow(); /* Call reset if not depending on the NextRow cleaning up. For example for select count(*) statements*/ bool reset(); bool RestartSelect(); private: //voID DecreaseRefCounter(); //int* ref_counter_; // not yet used... sqlite3_stmt* stmt_;};class sqliteWrapper { public: sqliteWrapper(); bool Open(std::string const& db_file); class ResultRecord { public: std::vector<std::string> fIElds_; }; class Resulttable { frIEnd class sqliteWrapper; public: Resulttable() : ptr_cur_record_(0) {} std::vector<ResultRecord> records_; ResultRecord* next() { if (ptr_cur_record_ < records_.size()) { return &(records_[ptr_cur_record_++]); } return 0; } private: voID reset() { records_.clear(); ptr_cur_record_=0; } private: unsigned int ptr_cur_record_; }; bool SelectStmt (std::string const& stmt,Resulttable& res); bool DirectStatement (std::string const& stmt); sqliteStatement* Statement(std::string const& stmt); std::string LastError(); // Transaction related bool Begin (); bool Commit (); bool Rollback(); private: static int SelectCallback(voID *p_data,int num_fIElds,char **p_fIElds,char **p_col_names); sqlite3* db_;};#endif
/* sqliteWrapper.cpp copyright (C) 2004 René Nyffenegger This source code is provIDed 'as-is',and must not be misrepresented as being the original source code. 3. This notice may not be removed or altered from any source distribution. René Nyffenegger rene.nyffenegger@adp-gmbh.ch*/#include "sqliteWrapper.h"// Todo: raus#include <windows.h>sqliteWrapper::sqliteWrapper() : db_(0) {}bool sqliteWrapper::Open(std::string const& db_file) { if (sqlite3_open(db_file.c_str(),&db_) != sqlITE_OK) { return false; } return true;} bool sqliteWrapper::SelectStmt(std::string const& stmt,Resulttable& res) { char *errmsg; int ret; res.reset(); ret = sqlite3_exec(db_,stmt.c_str(),SelectCallback,static_cast<voID*> (&res),&errmsg); if (ret != sqlITE_OK) { return false; } return true;// if (ret != sqlITE_OK) {// std::cout << stmt << " [" << errmsg << "]" << std::endl;// }}// Todo parameter p_col_namesint sqliteWrapper::SelectCallback(voID *p_data,char** p_col_names) { Resulttable* res = reinterpret_cast<Resulttable*>(p_data); ResultRecord record;#ifdef sqlITE_WRAPPER_REPORT_ColUMN_nameS // Hubert Castelain: column names in the first row of res if res is empty if(res->records_.size()==0) { ResultRecord col_names; for(int i=0; i < num_fIElds; i++) { if(p_fIElds[i]) col_names.fIElds_.push_back (p_col_names[i]); else col_names.fIElds_.push_back("(null)"); // or what else ? } res->records_.push_back(col_names); }#endif for(int i=0; i < num_fIElds; i++) { // Hubert Castelain: special treatment if null if (p_fIElds[i]) record.fIElds_.push_back(p_fIElds[i]); else record.fIElds_.push_back("<null>"); } res->records_.push_back(record); return 0;}sqliteStatement* sqliteWrapper::Statement(std::string const& statement) { sqliteStatement* stmt; try { stmt = new sqliteStatement(statement,db_); return stmt; } catch (const char* e) { return 0; }}sqliteStatement::sqliteStatement(std::string const& statement,sqlite3* db) { if ( sqlite3_prepare( db,statement.c_str(),// stmt -1,// If than zero,then stmt is read up to the first nul terminator &stmt_,0 // Pointer to unused portion of stmt ) != sqlITE_OK) { throw sqlite3_errmsg(db); } if (!stmt_) { throw "stmt_ is 0"; }}sqliteStatement::~sqliteStatement() { // Hubert Castelain 28/8/2005 // Prevent the database remaining locked after some statement. // Syntax: int sqlite3_finalize(sqlite3_stmt *pStmt); if(stmt_) sqlite3_finalize(stmt_);}sqliteStatement::sqliteStatement() : stmt_ (0){}bool sqliteStatement::Bind(int pos_zero_indexed,std::string const& value) { if (sqlite3_bind_text ( stmt_,pos_zero_indexed+1,// Index of wildcard value.c_str(),value.length(),// length of text sqlITE_TRANSIENT // sqlITE_TRANSIENT: sqlite makes its own copy ) != sqlITE_OK) { return false; } return true;}bool sqliteStatement::Bind(int pos_zero_indexed,double value) { if (sqlite3_bind_double( stmt_,// Index of wildcard value ) != sqlITE_OK) { return false; } return true;}bool sqliteStatement::Bind(int pos_zero_indexed,int value) { if (sqlite3_bind_int( stmt_,// Index of wildcard value ) != sqlITE_OK) { return false; } return true;}bool sqliteStatement::BindNull(int pos_zero_indexed) { if (sqlite3_bind_null( stmt_,pos_zero_indexed+1 // Index of wildcard ) != sqlITE_OK) { return false; } return true;}bool sqliteStatement::Execute() { int rc = sqlite3_step(stmt_); if (rc == sqlITE_BUSY) { ::MessageBox(0,"sqlITE_BUSY",0); return false; } if (rc == sqlITE_ERROR) { ::MessageBox(0,"sqlITE_ERROR",0); return false; } if (rc == sqlITE_MISUSE) { ::MessageBox(0,0); return false; } if (rc != sqlITE_DONE) { //sqlite3_reset(stmt_); return false; } sqlite3_reset(stmt_); return true;}sqliteStatement::dataType sqliteStatement::DataType(int pos_zero_indexed) { return dataType(sqlite3_column_type(stmt_,pos_zero_indexed));}int sqliteStatement::ValueInt(int pos_zero_indexed) { return sqlite3_column_int(stmt_,pos_zero_indexed);}std::string sqliteStatement::ValueString(int pos_zero_indexed) { return std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt_,pos_zero_indexed)));}bool sqliteStatement::RestartSelect() { sqlite3_reset(stmt_); return true;}bool sqliteStatement::reset() { int rc = sqlite3_step(stmt_); sqlite3_reset(stmt_); if (rc == sqlITE_ROW) return true; return false;}bool sqliteStatement::NextRow() { int rc = sqlite3_step(stmt_); if (rc == sqlITE_ROW ) { return true; } if (rc == sqlITE_DONE ) { sqlite3_reset(stmt_); return false; } else if (rc == sqlITE_MISUSE) { ::MessageBox(0,"sqliteStatement::NextRow sqlITE_MISUSE",0); } else if (rc == sqlITE_BUSY ) { ::MessageBox(0,"sqliteStatement::NextRow sqlITE_BUSY",0); } else if (rc == sqlITE_ERROR ) { ::MessageBox(0,"sqliteStatement::NextRow sqlITE_ERROR",0); } return false;}bool sqliteWrapper::DirectStatement(std::string const& stmt) { char *errmsg; int ret; ret = sqlite3_exec(db_,&errmsg); if(ret != sqlITE_OK) { return false; } return true; //if(ret != sqlITE_OK) { // std::cout << stmt << " [" << errmsg << "]" << std::endl; //}}std::string sqliteWrapper::LastError() { return sqlite3_errmsg(db_);}bool sqliteWrapper::Begin() { return DirectStatement("begin");}bool sqliteWrapper::Commit() { return DirectStatement("commit");}bool sqliteWrapper::Rollback() { return DirectStatement("rollback");}总结
以上是内存溢出为你收集整理的SQLite3封装类全部内容,希望文章能够帮你解决SQLite3封装类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)