1. 头文件
#pragma once #include <string> #include <sstream> using namespace std; #include "sqlite/sqlite3.h" #pragma comment(lib,"sqlite/sqlite3.lib") class CResultSet { public: friend class CsqliteMgr; public: CResultSet(); ~CResultSet(); public: int GetColumnNum(); // 获得结果数目 int GetRecordCount(); public: int GetInt(int row,int column); string GetString(int row,int column); private: // 获取对应行列的结果 char * GetAt(int row,int column); private: char **szResult; int nRow; int nColumn; }; class CsqliteMgr { public: CsqliteMgr(string filename); ~CsqliteMgr(); public: int Execute(const char *sql); int Execute(string sql); CResultSet * ExecuteQuery(string sql); public: void FreeResultSet(CResultSet *rs); public: void Begin(); void Commit(); void Rollback(); private: void print_error(char *error); private: sqlite3 *m_conn; };
2. 源文件
#include "stdafx.h" #include "sqliteMgr.h" CResultSet::CResultSet() { } CResultSet::~CResultSet() { ::sqlite3_free_table(szResult); } int CResultSet::GetColumnNum() { return nColumn; } int CResultSet::GetRecordCount() { return nRow; } int CResultSet::GetInt(int row,int column) { return atoi(GetAt(row,column)); } string CResultSet::GetString(int row,int column) { return string(GetAt(row,column)); } char * CResultSet::GetAt(int row,int column) { return szResult[(row + 1) * nColumn + column]; } CsqliteMgr::CsqliteMgr(string filename) { int nRet = ::sqlite3_open(filename.c_str(),&m_conn); if (nRet != sqlITE_OK) { print_error("sqlite3_open fail"); } } CsqliteMgr::~CsqliteMgr() { ::sqlite3_close(m_conn); } int CsqliteMgr::Execute(const char *sql) { char *error_msg = NULL; int nRet = ::sqlite3_exec(m_conn,sql,(char **) &error_msg); if (nRet != sqlITE_OK) { print_error(error_msg); return -1; } return nRet; } int CsqliteMgr::Execute(string sql) { return Execute(sql.c_str()); } CResultSet * CsqliteMgr::ExecuteQuery(string sql) { char *error_msg = NULL; CResultSet *pRs = new CResultSet; int nRet = ::sqlite3_get_table(m_conn,sql.c_str(),&pRs->szResult,&pRs->nRow,&pRs->nColumn,(char **) &error_msg); if (nRet != sqlITE_OK) { print_error(error_msg); return NULL; } return pRs; } void CsqliteMgr::FreeResultSet(CResultSet *rs) { if (rs != NULL) { delete rs; rs = NULL; } } void CsqliteMgr::Begin() { Execute("BEGIN TRANSACTION"); } void CsqliteMgr::Commit() { Execute("COMMIT TRANSACTION"); } void CsqliteMgr::Rollback() { Execute("ROLLBACK TRANSACTION"); } void CsqliteMgr::print_error(char *error) { char szError[128]; memset(szError,128); sprintf(szError,"sqlite error: %s/n",error); OutputDebugStringA(szError); sqlite3_free(error); }
3. 测试代码
#include <vector> #include <string> using namespace std; #include "sqliteMgr.h" #include "Tools.h" struct cRecord { int id; string name; int age; }; int _tmain(int argc,_TCHAR* argv[]) { sqliteMgr mgr("test.db"); if (false) { mgr.Execute("create table test([id] integer primary key autoincrement,[name] text,[age] int);"); char buf[256]; memset(buf,256); for (int i = 0; i < 100; ++i) { sprintf(buf,"insert into test(name,age) values(/"tom%d/",%d)",i + 1,i + 20); mgr.Execute(buf); } } else { ResultSet *rs = mgr.ExecuteQuery("select id,name,age from test"); vector<cRecord> records; for (int i = 0; i < rs->GetRecordCount(); ++i) { cRecord record; record.id = rs->GetInt(i,0); record.name = CTools::utf8_2_ansi(rs->GetString(i,1).c_str()); record.age = rs->GetInt(i,2); records.push_back(record); } mgr.FreeResultSet(rs); } return 0; }