一个Sqlite的封装
是一个仿照ado的封转类来写的,用来简化我的开发,性能还没有测试过……欢迎拍砖。
真正用的话,这里面还需要 sqlite3.def dll exp h lib 这五个文件,网上都有的。
// SQLite.h: interface for the CSQLite class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_SQLITE_H__68469ECF_69D1_42A6_8B65_D6DD85787F29__INCLUDED_) #define AFX_SQLITE_H__68469ECF_69D1_42A6_8B65_D6DD85787F29__INCLUDED_ #pragma comment(lib, "sqlite3.lib") #include "sqlite3.h" #pragma warning(disable:4146) #pragma warning(disable:4786) #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <list> #include <map> using namespace std; class CSQLite { public: BOOL initDB(const char *filename); CString GetValueByName(CString ColName); void MoveEnd(); void MoveFirst(); void MoveTo(int Position); void MoveFarwad(); void MoveNext(); BOOL isEnd(); BOOL isFirst(); BOOL isEmpty(); BOOL ExecSQL(const char *sql); CSQLite(); CSQLite(char *filename); virtual ~CSQLite(); private: sqlite3 *m_db; typedef map<CString , CString> ValueMap; map<int,ValueMap> m_valueList; ValueMap m_Value; int m_setCount; int m_setPosition; static int _callback_exec(void *nouse,int argc, char ** argv, char ** aszColName); protected: }; #endif // !defined(AFX_SQLITE_H__68469ECF_69D1_42A6_8B65_D6DD85787F29__INCLUDED_)
// SQLite.cpp: implementation of the CSQLite class. // ////////////////////////////////////////////////////////////////////// //author:CuiXS //QQ:cui.xs@qq.com //date:20101201 //请建立数据库先~ //初始化数据库可以采用构造函数也可以使用initDB(sqlite文件名) //执行SQL语句ExecSQL(SQL) //返回值会按顺序保存在一个map<索引,map<属性名,属性值>>中 //取结果之前请先判断是不是空的,并且是不是已经到了最前或者最后 //while(!db.isEmpty() && db.isFirst() && db.isEnd()) //取结果GetValueByName(属性名) //移动结果集 向后:MoveNext() 向前:MoveFaward() 第一:MoveFirst 最后:MoveEnd //移动到特定结果集 MoveTo(Position) 结果集是从0开始的。 #include "stdafx.h" #include "testsqlite.h" #include "SQLite.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSQLite::CSQLite() { } CSQLite::CSQLite(char *filename) { initDB(filename); } CSQLite::~CSQLite() { } BOOL CSQLite::initDB(const char *filename) { int ret = 0; ret = sqlite3_open(filename, &m_db); if ( ret != SQLITE_OK ) { CString errorMsg; errorMsg.Format("Open sqlite3 DataBase \"%s\" Failed",filename); AfxMessageBox(errorMsg); return FALSE; } m_setCount = 0; m_setPosition = 0; return TRUE; } BOOL CSQLite::ExecSQL(const char *sql) { CString errorMsg; char * pErrMsg = 0; int ret; ret = sqlite3_exec(m_db,sql,_callback_exec,this,&pErrMsg ); if ( ret != SQLITE_OK ) { errorMsg.Format("ERROR CODE %d ERROR MESSAGE: %s",ret,pErrMsg); AfxMessageBox(errorMsg); return FALSE; } return TRUE; } int CSQLite::_callback_exec(void *nouse, int argc, char **argv, char **aszColName) { //每条记录返回一个callback_exec //使用map进行存储 CSQLite *my = (CSQLite *)nouse; int i = 0; CString key,value; while(i < argc) { key.Format("%s",aszColName[i]); value.Format("%s",argv[i]); my->m_Value[key] = value; i++; } my->m_valueList[my->m_setCount++] = my->m_Value; return 0; } BOOL CSQLite::isEmpty() { if (m_setCount == 0) { return TRUE; } return FALSE; } BOOL CSQLite::isFirst() { if (m_setPosition == -1) { return TRUE; } return FALSE; } BOOL CSQLite::isEnd() { if (m_setPosition == m_setCount) { return TRUE; } return FALSE; } void CSQLite::MoveNext() { m_setPosition++; } void CSQLite::MoveFarwad() { m_setPosition--; } void CSQLite::MoveTo(int Position) { if(Position < m_setCount && Position >= 0) { m_setPosition = Position; } else { AfxMessageBox("MoveTo Position Failed!"); } } void CSQLite::MoveFirst() { m_setPosition = 0; } void CSQLite::MoveEnd() { m_setPosition = m_setCount-1; } CString CSQLite::GetValueByName(CString ColName) { return m_valueList[m_setPosition][ColName]; }