赞
踩
sqlcipher是sqlite的加版本,分为免费版和收费版。
这里研究的是开源的免费版
https://github.com/sqlcipher/sqlcipher
Android码源默认提供了sqlite的native,jni和java版本,但没有提供sqlcipher,开发用到需要自己添加。
sqlcipher需要配置openssl,写一个android.bp
-
-
- android_sqlite_cflags = [
- "-DHAVE_USLEEP=1",
- "-DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576",
- "-DSQLITE_THREADSAFE=1",
- "-DNDEBUG=1",
- "-DSQLITE_ENABLE_MEMORY_MANAGEMENT=1",
- "-DSQLITE_TEMP_STORE=3",
- "-DSQLITE_ENABLE_FTS3_BACKWARDS",
- "-DSQLITE_ENABLE_LOAD_EXTENSION",
- "-DSQLITE_ENABLE_MEMORY_MANAGEMENT",
- "-DSQLITE_ENABLE_COLUMN_METADATA",
- "-DSQLITE_ENABLE_FTS4",
- "-DSQLITE_ENABLE_UNLOCK_NOTIFY",
- "-DSQLITE_ENABLE_RTREE",
- "-DSQLITE_SOUNDEX",
- "-DSQLITE_ENABLE_STAT3",
- "-DSQLITE_ENABLE_FTS4_UNICODE61",
- "-DSQLITE_THREADSAFE",
- "-Wno-unused-parameter",
- "-Wno-pointer-arith",
- "-Wno-unused-variable"
- ]
-
- sqlcipher_cflags = [
- "-DSQLITE_HAS_CODEC",
- "-DHAVE_FDATASYNC=0",
- "-Dfdatasync=fsync",
- ]
-
- sqlcipher_files = ["src/sqlite3.c"]
-
- //APP_STL := c++_shared
- //APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
- //APP_PLATFORM := android-26
-
- // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_EXPORT_C_INCLUDES
- // LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/
-
- //LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/arm64-v8a/libcrypto.so
- // ANDROIDMK TRANSLATION ERROR: unsupported include
- // include $(PREBUILT_SHARED_LIBRARY)
- cc_prebuilt_library_shared {
- name: "libcrypto2",
- target: {
- android_arm: {
- srcs: ["lib/armeabi-v7a/libcrypto.so"],
- },
- android_arm64: {
- srcs: ["lib/arm64-v8a/libcrypto.so"],
- },
- },
- strip: {
- none:true,
- },
- //shared_libs: ["libx", "libxx", "libxxx", "libxxxx"],
- check_elf_files: false,
- compile_multilib: "both"//32位和64位都预编译
- }
-
- cc_library_shared {
- name: "sqlite3_ext2",
- host_ldlibs: ["-llog"],
- srcs: sqlcipher_files,
- local_include_dirs: [
- "src",
- "include",
- ],
- cflags: android_sqlite_cflags + sqlcipher_cflags,
- //LOCAL_STATIC_LIBRARIES := static-libcrypto
- shared_libs: ["libcrypto2", "liblog",],
-
- }
详细使用看:
GitHub - blogercn/android_sqlciphe: android natvity C/C++使用的加密数据库sqlciphe
可以用于ndk,可以复制到android源码供C++层开发使用
sqlite数据库修改数据需要提交事务,否则显示成功,但并没有入库。
sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL);
if (rc != SQLITE_OK)
{
std::cout << "EventLogServer SQL错误: " << errMsg << std::endl;
sqlite3_free(errMsg);
rc = sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr);
}
else
{
rc = sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
}
如下:
int IEventLogServer::insertLog(slog& log)
{
sqlite3* db;
int rc = sqlite3_open(LOG_DATABASE, &db);
if (rc != SQLITE_OK)
{
std::cout << "EventLogServer 无法打开数据库: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return rc;
}
rc = sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL); // 开始事务
if (rc != SQLITE_OK) {
std::cout << "Failed to begin transaction: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return rc;
}
char* errMsg = nullptr;
std::string sql = "INSERT INTO " + std::string(LOG_TABLE) +" (Severity, EventID, BaseEventCount, TimeStamp, Source) VALUES (" +
std::to_string(log.Severity) + ", " + std::to_string(log.EventID) + ", " +
std::to_string(log.BaseEventCount) + ", " + std::to_string(log.TimeStamp) +", '" + log.Source + "');";
rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg);
if (rc != SQLITE_OK)
{
std::cout << "EventLogServer SQL错误: " << errMsg << std::endl;
sqlite3_free(errMsg);
rc = sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr);
}
else
{
rc = sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
}
sqlite3_close(db);
return rc;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。