StringConvert见https://blog.csdn.net/wxd090108/article/details/84190762
下载地址:https://sourceforge.net/projects/wxcode/files/Components/wxSQLite3/
下载wxsqlite3-***.zip
解压wxsqlite3-***.zip,获取wxsqlite3-***.zip\sqlite3\secure\src下所有文件
1、新建一个空dll项目sqlite3:
2、解压wxsqlite3-***.zip,获取 wxsqlite3-3.3.1\\wxsqlite3-3.3.1\\sqlite3\\secure\\src下的所有文件,将文件放置项目sqlite3的根目录下
3、包含sqlite3secure.c到项目中
4、生成项目sqlite3
5、右键项目sqlite3——属性——c/c++——Preprocessor,在Preprocessor Definitions中添加下面预编译:
SQLITE_HAS_CODEC=1
CODEC_TYPE=CODEC_TYPE_AES128
SQLITE_CORE
THREADSAFE
SQLITE_SECURE_DELETE
SQLITE_SOUNDEX
SQLITE_ENABLE_COLUMN_METADATA
6、在sqlite3.c和sqlite3secure.c 的头部加入:
#ifndef SQLITE_API
#define SQLITE_API __declspec(dllexport)
#endif
#ifndef SQLITE_HAS_CODEC
#define SQLITE_HAS_CODEC
#endif
7、在sqlite3.h的头部加入:
#ifndef SQLITE_API
#define SQLITE_API __declspec(dllimport)
#endif
#ifndef SQLITE_HAS_CODEC
#define SQLITE_HAS_CODEC
#endif
8、重新生成项目sqlite3
#include "stdafx.h"
#include "sqlite3.h"
#include "sqlite3userauth.h"
#include "StringConvert.h"//见博主博文——字符串编码转换
//加密
void Encrypt(sqlite3 * pDB, const string& dbpath,const string & key);
//解密
void Decode(sqlite3 * pDB, const string& dbpath,const string & key);
//删除密码
void DeletePassword(sqlite3 * pDB, const string& dbpath,const string & key);
int _tmain(int argc, _TCHAR* argv[])
{
string path="C:\\Users\\WXD\\Desktop\\sqlite3\\hytsystem.db";
string key = "abcd";
sqlite3 * pDB = NULL;
int dbopen=-1,nRes = -1,index=0;
//加密
Encrypt(pDB,path,key);
//解密
Decode(pDB,path,key);
//删除密码
DeletePassword(pDB,path,key);
return 0;
}
//加密
void Encrypt(sqlite3 * pDB, const string& dbpath,const string & key)
{
int dbopen=-1;
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
string utf8path = AsciiToUtf8(dbpath);//StringConvert.h中
dbopen=sqlite3_open_v2(utf8path.c_str(),&pDB, SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_READWRITE, nullptr);
if (dbopen != SQLITE_OK)
{
sqlite3_close(pDB);
return ;
}
dbopen=sqlite3_key(pDB,"",0);
dbopen=sqlite3_rekey(pDB,key.c_str(),key.length());
if (dbopen != SQLITE_OK)
{
sqlite3_close(pDB);
return ;
}
sqlite3_close(pDB);
}
//解密
void Decode(sqlite3 * pDB, const string& dbpath,const string & key)
{
int dbopen=-1, nRes=-1;
int nRow=0, nColumn=0, nIndex=0;
sqlite3 * pDB2 = NULL;
char** dbResult=nullptr;
char* errMes=nullptr;
string utf8path = AsciiToUtf8(dbpath);//StringConvert.h中
dbopen=sqlite3_open_v2(utf8path.c_str(),&pDB, SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_READWRITE, nullptr);
if (dbopen != SQLITE_OK)
{
sqlite3_close(pDB2);
return ;
}
dbopen=sqlite3_key(pDB,key.c_str(),key.length());
nRes=sqlite3_exec(pDB,"PRAGMA synchronous = OFF",0,0,0); //提高性能
nRes=sqlite3_exec(pDB,"PRAGMA cache_size = 8000",0,0,0); //加大缓存
nRes=sqlite3_exec(pDB,"PRAGMA count_changes = 1",0,0,0); //返回改变记录数
nRes=sqlite3_exec(pDB,"PRAGMA case_sensitive_like = 1",0,0,0); //支持中文LIKE查询
if (dbopen != SQLITE_OK)
{
sqlite3_close(pDB2);
return ;
}
nRes = sqlite3_exec(pDB,"CREATE table axue (name varchar(32),age int)",0,0,0);
nRes = sqlite3_exec(pDB,"insert into axue (name,age) values('axue',18)",0,0,0);
nRes = sqlite3_get_table(pDB,"Select * from axue",&dbResult,&nRow,&nColumn,&errMes);
nIndex+=nColumn;//第一行为字段名
for (int i=0; i