sqlite3 数据库加密:sqlite3mc

命令简单使用:

// 加密,将 test.db 加密后导出新的数据库 test_enc.db
user > sqlite3mc test.db
sqlite> ATTACH DATABASE 'test_enc.db' AS test_enc KEY 'xxxxxxxx'; --设置密码
sqlite> SELECT sqlcipher_export('test_enc');
sqlite> DETACH DATABASE test_enc;
sqlite> .exit

// 解密,将 test_enc.db 设置为空密码后导出 test.db
user > sqlite3mc test_enc.db
sqlite> PRAGMA key = 'xxxxxxxx';
selite> select * from test_table; --sql语句
sqlite> ATTACH DATABASE 'test.db' AS test KEY '';  --空的密码不会加密数据库
sqlite> SELECT sqlcipher_export('test');
sqlite> DETACH DATABASE test;
sqlite> .exit

样例代码:

#include 
#include 
#include 
#include 
#include 
#pragma comment(lib,"sqlite3mc_static_x64.lib")
int main() {
    sqlite3* db;
    sqlite3_open("test.db", &db);
    sqlite3_key(db, "111111", 6);
    sqlite3_exec(db, "create table person (id int, name vchar(50))", NULL, NULL, NULL);
    int i = sqlite3_exec(db, "insert into person (id,name) values (234,'quit')", NULL, NULL, NULL);
    printf("i=%d", i);
    sqlite3_close(db);
}

你可能感兴趣的:(数据库,sqlite,sqlite3加密)