QT使用SQLiteCipher对SQLite数据库进行加密的用法 代码显示
时间: 2024-05-08 21:17:30 浏览: 168
Qt中 SQlite 数据库 加密 SqliteCipher 实例
5星 · 资源好评率100%
以下是使用SQLiteCipher对SQLite数据库进行加密的示例代码:
1. 导入SQLiteCipher库
```C++
#include "sqlite3.h"
#include "sqlitecipher.h"
```
2. 打开数据库并设置密码
```C++
sqlite3 *db;
int rc = sqlite3_open("example.db", &db);
// 设置密码
rc = sqlite3_key(db, "password", strlen("password"));
```
3. 创建表和插入数据
```C++
// 创建表
rc = sqlite3_exec(db, "CREATE TABLE test (id INT, name TEXT)", NULL, NULL, NULL);
// 插入数据
rc = sqlite3_exec(db, "INSERT INTO test (id, name) VALUES (1, 'John')", NULL, NULL, NULL);
```
4. 查询数据
```C++
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, "SELECT * FROM test", -1, &stmt, NULL);
// 遍历结果集
while (sqlite3_step(stmt) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *name = sqlite3_column_text(stmt, 1);
printf("id: %d, name: %s\n", id, name);
}
sqlite3_finalize(stmt);
```
5. 关闭数据库
```C++
sqlite3_close(db);
```
阅读全文