SQLITE数据库C++封装类
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
bool OpenDB( const char *path ); bool CloseDB();
bool ExecSql( const char *szsql ); char* GetError(); static int FunSqlExec(void *buf, int nData, char **szValue, char **szName );
//cpp
#include "DataBase.h"
int CDataBase::m_nHandleCount = 0; CDataBase* CDataBase::m_pHandle = NULL; CDataBase::CDataBase(void) {
meห้องสมุดไป่ตู้set( m_szErrMsg, 0, MAX_SIZE_BUF ); m_pdb = NULL; m_pHandle = NULL; }
char* CDataBase::GetError() {
return m_szErrMsg; }
int CDataBase::GetRecordSetSize() {
return m_Recordset.size(); }
void CDataBase::GetRecordSetValueByIndex( unsigned int nIndex, PSINGLERECORD pValue)
{ if ( m_Recordset.size() <= nIndex ) { return; } SINGLERECORD::iterator iter = m_Recordset[nIndex].begin(); for ( ; iter != m_Recordset[nIndex].end(); iter ++ ) { pValue->insert( (*iter) ); }
int GetRecordSetSize(); void GetRecordSetValueByIndex( unsigned int nIndex, PSINGLERECORD pValue); string GetValueByIndexAndKey( unsigned int nIndex, string strKey ); //增删改查 };
CDataBase(void); ~CDataBase(void); static int m_nHandleCount; static CDataBase *m_pHandle; struct sqlite3* m_pdb; RECORDSET m_Recordset; char m_szErrMsg[MAX_SIZE_BUF]; public: static CDataBase* GetDataBaseHandle(); void ReleaseDataBaseHandle();
if( SQLITE_OK != sqlite3_open( path, &m_pdb) ) {
SQLITE_LAST_ERRNO; return false; } else { sqlite3_exec(m_pdb, "PRAGMA synchronous = OFF; ", 0,0,0); return true; } }
CDataBase::~CDataBase(void) { }
CDataBase* CDataBase::GetDataBaseHandle() {
if ( m_pHandle == NULL ) {
m_pHandle = new CDataBase; if ( m_pHandle != NULL ) {
}
string CDataBase::GetValueByIndexAndKey( unsigned int nIndex, string strKey ) {
string str; if ( m_Recordset.size() <= nIndex ) {
return str; } SINGLERECORD::iterator iter = m_Recordset[nIndex].begin(); for ( ; iter != m_Recordset[nIndex].end(); iter ++ ) {
m_nHandleCount --; } if ( m_nHandleCount == 0 ) {
if ( m_pHandle != NULL ) {
delete m_pHandle; m_pHandle = NULL; } } }
bool CDataBase::OpenDB( const char *path ) {
strcpy( m_szErrMsg, pErr ); return false; } return true; }
int CDataBase::FunSqlExec( void *buf, int nData, char **szValue, char **szName ) {
CDataBase* pDb = (CDataBase*)buf; map<string, string> rec; string strName, strValue;
for ( int i = 0; i < nData; i ++ ) {
strName = szName[i%nData]; if ( szValue[i] != NULL ) {
strValue = szValue[i]; } else {
strValue = "0"; } rec.insert( make_pair<string, string>(strName, strValue) ); } pDb->m_Recordset.push_back( rec ); return 0; }
自己封装的一个简单的sqlite操作类.喜欢的拿走. //头文件 #pragma once #include <vector> #include <map> #include <string> #include "sqlite/sqlite3.h" using namespace std; typedef map<string, string> SINGLERECORD, *PSINGLERECORD; typedef vector<SINGLERECORD> RECORDSET; #define MAX_SIZE_BUF 128 class CDataBase { private:
bool CDataBase::CloseDB() {
if ( SQLITE_OK != sqlite3_close(m_pdb) ) {
return false; } else {
return true; } }
bool CDataBase::ExecSql( const char *szsql ) {
memset( m_szErrMsg, 0, MAX_SIZE_BUF ); m_Recordset.clear(); char *pErr = m_szErrMsg; if ( SQLITE_OK != sqlite3_exec(m_pdb, szsql, &CDataBase::FunSqlExec, this, &pErr)) {
m_nHandleCount ++; return m_pHandle; } return NULL; } else { m_nHandleCount ++; return m_pHandle; } }
void CDataBase::ReleaseDataBaseHandle() {
if ( m_nHandleCount > 0 ) {
if ( iter->first == strKey ) {
return iter->second; } } return str; }
bool ExecSql( const char *szsql ); char* GetError(); static int FunSqlExec(void *buf, int nData, char **szValue, char **szName );
//cpp
#include "DataBase.h"
int CDataBase::m_nHandleCount = 0; CDataBase* CDataBase::m_pHandle = NULL; CDataBase::CDataBase(void) {
meห้องสมุดไป่ตู้set( m_szErrMsg, 0, MAX_SIZE_BUF ); m_pdb = NULL; m_pHandle = NULL; }
char* CDataBase::GetError() {
return m_szErrMsg; }
int CDataBase::GetRecordSetSize() {
return m_Recordset.size(); }
void CDataBase::GetRecordSetValueByIndex( unsigned int nIndex, PSINGLERECORD pValue)
{ if ( m_Recordset.size() <= nIndex ) { return; } SINGLERECORD::iterator iter = m_Recordset[nIndex].begin(); for ( ; iter != m_Recordset[nIndex].end(); iter ++ ) { pValue->insert( (*iter) ); }
int GetRecordSetSize(); void GetRecordSetValueByIndex( unsigned int nIndex, PSINGLERECORD pValue); string GetValueByIndexAndKey( unsigned int nIndex, string strKey ); //增删改查 };
CDataBase(void); ~CDataBase(void); static int m_nHandleCount; static CDataBase *m_pHandle; struct sqlite3* m_pdb; RECORDSET m_Recordset; char m_szErrMsg[MAX_SIZE_BUF]; public: static CDataBase* GetDataBaseHandle(); void ReleaseDataBaseHandle();
if( SQLITE_OK != sqlite3_open( path, &m_pdb) ) {
SQLITE_LAST_ERRNO; return false; } else { sqlite3_exec(m_pdb, "PRAGMA synchronous = OFF; ", 0,0,0); return true; } }
CDataBase::~CDataBase(void) { }
CDataBase* CDataBase::GetDataBaseHandle() {
if ( m_pHandle == NULL ) {
m_pHandle = new CDataBase; if ( m_pHandle != NULL ) {
}
string CDataBase::GetValueByIndexAndKey( unsigned int nIndex, string strKey ) {
string str; if ( m_Recordset.size() <= nIndex ) {
return str; } SINGLERECORD::iterator iter = m_Recordset[nIndex].begin(); for ( ; iter != m_Recordset[nIndex].end(); iter ++ ) {
m_nHandleCount --; } if ( m_nHandleCount == 0 ) {
if ( m_pHandle != NULL ) {
delete m_pHandle; m_pHandle = NULL; } } }
bool CDataBase::OpenDB( const char *path ) {
strcpy( m_szErrMsg, pErr ); return false; } return true; }
int CDataBase::FunSqlExec( void *buf, int nData, char **szValue, char **szName ) {
CDataBase* pDb = (CDataBase*)buf; map<string, string> rec; string strName, strValue;
for ( int i = 0; i < nData; i ++ ) {
strName = szName[i%nData]; if ( szValue[i] != NULL ) {
strValue = szValue[i]; } else {
strValue = "0"; } rec.insert( make_pair<string, string>(strName, strValue) ); } pDb->m_Recordset.push_back( rec ); return 0; }
自己封装的一个简单的sqlite操作类.喜欢的拿走. //头文件 #pragma once #include <vector> #include <map> #include <string> #include "sqlite/sqlite3.h" using namespace std; typedef map<string, string> SINGLERECORD, *PSINGLERECORD; typedef vector<SINGLERECORD> RECORDSET; #define MAX_SIZE_BUF 128 class CDataBase { private:
bool CDataBase::CloseDB() {
if ( SQLITE_OK != sqlite3_close(m_pdb) ) {
return false; } else {
return true; } }
bool CDataBase::ExecSql( const char *szsql ) {
memset( m_szErrMsg, 0, MAX_SIZE_BUF ); m_Recordset.clear(); char *pErr = m_szErrMsg; if ( SQLITE_OK != sqlite3_exec(m_pdb, szsql, &CDataBase::FunSqlExec, this, &pErr)) {
m_nHandleCount ++; return m_pHandle; } return NULL; } else { m_nHandleCount ++; return m_pHandle; } }
void CDataBase::ReleaseDataBaseHandle() {
if ( m_nHandleCount > 0 ) {
if ( iter->first == strKey ) {
return iter->second; } } return str; }