#include<iostream>
#include<string>
#include<sqlcipher/sqlite3.h>
#include<sqlcipher/sqlite3_wcdb.h>
static
std::string bytesFromHex(
const
std::string& _Src) {
std::string _Out;
for
(unsigned
int
i = 0; i < _Src.length(); i += 2) {
_Out.push_back(std::stoi(_Src.substr(i, 2), 0, 16));
}
return
_Out;
}
static
int
callback(
void
* notUsed,
int
argc,
char
** argv,
char
** azColName)
{
for
(
int
i = 0; i < argc; i++)
std::cout << azColName[i] <<
":"
<< (argv[i] ? argv[i] :
"NULL"
) <<
"\t"
;
std::cout << std::endl;
return
0;
}
int
main() {
int
rc = 0;
std::string szDbKey =
"0C1ADFFBE53345BAA59C748B7660EAC8542AD78C64FC422F9B278E5336EE3EB0"
;
std::string binDbKey = bytesFromHex(szDbKey);
std::string szDbPath = R
"(D:\CPLUSPLUS\wcdb_test\MicroMsg.db)"
;
sqlite3* db = nullptr;
char
* errmsg = nullptr;
char
* zKey = nullptr;
int
nKey = 0;
int
index = 0;
rc = sqlite3_open_v2(szDbPath.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAINDB_READONLY, 0);
if
(rc != SQLITE_OK) {
std::cout <<
"打开或创建数据库失败"
<< std::endl;
goto
wrapup;
}
rc = sqlite3_exec(db, R
"(PRAGMA wal_autocheckpoint = 0;)"
, nullptr, nullptr, &errmsg);
std::cout << rc << std::endl;
rc = sqlite3_exec(db, R
"(PRAGMA checkpoint_fullfsync = true;)"
, nullptr, nullptr, &errmsg);
std::cout << rc << std::endl;
rc = sqlite3_exec(db, R
"(PRAGMA temp_store = 1;)"
, nullptr, nullptr, &errmsg);
std::cout << rc << std::endl;
rc = sqlite3_key(db, binDbKey.c_str(), (
int
)binDbKey.size());
std::cout << rc << std::endl;
rc = sqlite3_exec(db, R
"(PRAGMA cipher_compatibility = 3;)"
, nullptr, nullptr, &errmsg);
std::cout << rc << std::endl;
rc = sqlite3_exec(db, R
"(PRAGMA cipher_page_size = 4096;)"
, nullptr, nullptr, &errmsg);
std::cout << rc << std::endl;
rc = sqlite3_exec(db, R
"(PRAGMA journal_mode = "
WAL
";)"
, nullptr, nullptr, &errmsg);
std::cout << rc << std::endl;
index = sqlcipher_find_db_index(db,
"binary"
);
sqlite3CodecGetKey(db, index, (
void
**)&zKey, &nKey);
rc = sqlite3_exec(db,
"SELECT COUNT(*) FROM Contact;"
, callback, NULL, &errmsg);
if
(rc != SQLITE_OK) {
std::cout <<
"SQL语句执行时发生错误: "
<< errmsg << std::endl;
goto
wrapup;
}
wrapup:
rc = sqlite3_close(db);
std::cout << rc << std::endl;
return
0;
}