程序中使用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,const char *sql,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,0,dbpath,-1,NULL,0,NULL,NULL);
char *dbpathA = new char[len+1];
memset(dbpathA,0,len+1);
WideCharToMultiByte(CP_UTF8,0,dbpath,-1,dbpathA,len,NULL,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,int n_columns,char **column_values,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,0,&zErrMsg);
MessageBoxA(NULL,g_tablename.c_str(),"msg",0);
}
else
{
res=(m_SqliteExec)(m_dbhandle,sqlstate,0,0,&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,sqlstate,0,0,&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;
}