程序中使用sqlite数据库存储数据,每次打包程序之前都要查看一下数据表中是否有垃圾数据,手动清除,很麻烦,所有编写了一个自动清除sqlite数据库中的所有表的数据的类
代码如下,欢迎指正:
#pragma once #include <string> using namespace std; //数据库中表的名称 extern string g_tablename; //数据库中表的内容 extern int g_tablecnt; //导出sqlite3数据库函数 typedef int (__cdecl *MysqLITEOPEN)(const char* filename,HANDLE **dbHandle); typedef int (__cdecl *MysqLITEEXEC)(HANDLE *dbHandle,const char *sql,int (*callback)(void*,int,char**,char**),void *,char **errmsg); typedef int (__cdecl *MysqLITEPREP)(HANDLE *dbHandle,int nBytes,char **ppStmt,char **pzTail); typedef int (__cdecl *MysqLITESTEP)(char *ppStmt); typedef int (__cdecl *MysqLITECLOSE)(HANDLE *dbHandle); int printresult(void *data,int n_columns,char **column_values,char **column_names); class COpsqlitedb { public: COpsqlitedb(void); ~COpsqlitedb(void); public: int LoadsqliteLibrary(wchar_t* dbpath); int FreesqliteLibrary(); int Opensqlitedb(wchar_t *dbpath); int Closesqlitedb(); int ExecsqlState(char* sqlstate,bool bneedcallback); int CleartableData(); private: HANDLE *m_dbhandle; HINSTANCE m_Hinstance; MysqLITEOPEN m_sqliteOpen; MysqLITEEXEC m_sqliteExec; MysqLITEPREP m_sqliteprep; MysqLITECLOSE m_sqliteClose; };
#include "StdAfx.h" #include "opsqlitedb.h" //数据库中表的名称 string g_tablename=""; //数据库中表的内容 int g_tablecnt=0; COpsqlitedb::COpsqlitedb(void) { m_dbhandle=NULL; m_Hinstance = NULL; m_sqliteOpen=NULL; m_sqliteExec=NULL; m_sqliteprep=NULL; m_sqliteClose=NULL; } COpsqlitedb::~COpsqlitedb(void) { m_dbhandle=NULL; m_Hinstance = NULL; m_sqliteOpen=NULL; m_sqliteExec=NULL; m_sqliteprep=NULL; m_sqliteClose=NULL; } int COpsqlitedb::LoadsqliteLibrary(wchar_t* dbpath) { bool FileExistRes = PathFileExists(dbpath); if(!FileExistRes) { return -1; } m_Hinstance = LoadLibrary(dbpath); if(m_Hinstance!=NULL) { m_sqliteOpen =(MysqLITEOPEN)GetProcAddress(m_Hinstance,"sqlite3_open"); m_sqliteprep = (MysqLITEPREP)GetProcAddress(m_Hinstance,"sqlite3_prepare_v2"); m_sqliteExec = (MysqLITEEXEC)GetProcAddress(m_Hinstance,"sqlite3_exec"); m_sqliteClose =(MysqLITECLOSE)GetProcAddress(m_Hinstance,"sqlite3_close"); if((NULL==m_sqliteOpen) || (NULL==m_sqliteprep) || (NULL==m_sqliteExec)||(NULL==m_sqliteClose)) { return -1; } } return 0; } int COpsqlitedb::FreesqliteLibrary() { FreeLibrary(m_Hinstance); m_Hinstance = NULL; return 0; } int COpsqlitedb::Opensqlitedb(wchar_t *dbpath) { if (m_sqliteOpen) { int len = WideCharToMultiByte(CP_UTF8,dbpath,-1,NULL,NULL); char *dbpathA = new char[len+1]; memset(dbpathA,len+1); WideCharToMultiByte(CP_UTF8,dbpathA,len,NULL); dbpathA[len]='\0'; int rc = (m_sqliteOpen)(dbpathA,&m_dbhandle); delete[] dbpathA; return rc; } else return -1; } int COpsqlitedb::Closesqlitedb() { return (m_sqliteClose)(m_dbhandle); } int printresult(void *data,char **column_names) { g_tablecnt += n_columns; for (int i=0;i<n_columns;i++) { //MessageBoxA(NULL,column_names[i],column_values[i],0); g_tablename += column_values[i]; g_tablename+=";"; } return 0; } int COpsqlitedb::ExecsqlState(char* sqlstate,bool bneedcallback) { char *zErrMsg = '\0'; int res = -1; if (bneedcallback) { g_tablecnt = 0; g_tablename = ""; res=(m_sqliteExec)(m_dbhandle,sqlstate,printresult,&zErrMsg); MessageBoxA(NULL,g_tablename.c_str(),"msg",0); } else { res=(m_sqliteExec)(m_dbhandle,&zErrMsg); } if (res) { MessageBoxA(NULL,"operator db fail",zErrMsg,0); } return res; } int COpsqlitedb::CleartableData() { int pos= 0; int res = -1; unsigned int size=g_tablename.size(); for(unsigned int i=0; i<size; i++) { pos=g_tablename.find(";",i); if(pos<size) { string s=g_tablename.substr(i,pos-i); char *zErrMsg = '\0'; char sqlstate[MAX_PATH] = {"0"}; sprintf_s(sqlstate,sizeof(sqlstate),"DELETE FROM %s",s.c_str()); res=(m_sqliteExec)(m_dbhandle,&zErrMsg); i=pos; } } return res; }
int dropdbdata(wchar_t *dllpath,wchar_t* dbpath) { COpsqlitedb operatedb; int res = operatedb.LoadsqliteLibrary(dllpath); if (res) { return res; } res = operatedb.Opensqlitedb(dbpath); if (res) { operatedb.FreesqliteLibrary(); return res; } res = operatedb.ExecsqlState("select name from sqlite_master where type='table' order by name;",true); if (res) { operatedb.Closesqlitedb(); operatedb.FreesqliteLibrary(); return res; } res = operatedb.CleartableData(); if (res) { operatedb.Closesqlitedb(); operatedb.FreesqliteLibrary(); return res; } res = operatedb.Closesqlitedb(); res = operatedb.FreesqliteLibrary(); return res; }