加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite数据库在程序打包之前清除所有表中的数据

发布时间:2020-12-12 19:33:55 所属栏目:百科 来源:网络整理
导读:程序中使用sqlite数据库存储数据,每次打包程序之前都要查看一下数据表中是否有垃圾数据,手动清除,很麻烦,所有编写了一个自动清除sqlite数据库中的所有表的数据的类 代码如下,欢迎指正: #pragma once#include stringusing namespace std;//数据库中表的

程序中使用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]='';
		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 = '';
	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 = '';
			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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读