对不起,我首先缺少英语。
我编译它是因为我需要Android NDK中的SQLite3扩展JSON1
gcc -Os -I. -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_JSON1 \
-DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_USE_ALLOCA \
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
-DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_OMIT_DECLTYPE \
-DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE \
-DSQLITE_OMIT_DEPRECATED -DHAVE_USLEEP -DHAVE_READLINE \
shell.c sqlite3.c -ldl -lreadline -lncurses -o sqlite3
编译结果为sqlite3.exe
如何将此文件用于Anroid NDK 还是可以找到其他方式?
我要使用的代码
JNIEXPORT void JNICALL
Java_com_test_ndkapp_MainActivity_SQLiteCPP(
JNIEnv *jenv,
jobject self,
jstring database_path
){
sqlite3 *db;
sqlite3_stmt *stmt;
// jstring to const char *
const char *database = jenv->GetStringUTFChars(database_path, 0);
char *zErrMsg = 0;
int rc;
//SQL ERROR:no such function: json
const char * create_sql = "create table user(name, phone)";
const char * insert_sql = "insert into user (name, phone) values(\"oz\", json('{\"cell\":\"+491765\", \"home\":\"+498973\"}'))";
const char * select_sql = "select json_extract(user.phone, '$.cell') from user";
if (sqlite3_open_v2(database, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , NULL) == SQLITE_OK) {
LOGE("database open!!");
rc = sqlite3_exec(db, create_sql, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ) {
LOGE("SQL ERROR:%s", zErrMsg);
sqlite3_free(zErrMsg);
} else {
LOGE("CREATE Operation done successfully\n");
}
rc = sqlite3_exec(db, insert_sql, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ) {
LOGE("SQL ERROR:%s", zErrMsg);
sqlite3_free(zErrMsg);
} else {
LOGE("INSERT Operation done successfully\n");
}
rc = sqlite3_prepare_v2(db, select_sql, -1, &stmt, NULL);
if( rc != SQLITE_OK ) {
LOGE("SQL ERROR:%s", zErrMsg);
sqlite3_free(zErrMsg);
} else {
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const unsigned char * aaa = sqlite3_column_text (stmt, 0);
const unsigned char * bbb = sqlite3_column_text (stmt, 1);
LOGE("%s, %s", aaa, bbb);
}
if (rc != SQLITE_DONE) {
LOGE("error: %s", sqlite3_errmsg(db));
}
LOGE("SELECT Operation done successfully\n");
sqlite3_finalize(stmt);
}
} else {
LOGE("database can not open!! : %s", sqlite3_errmsg(db));
}
sqlite3_close(db);
}
我尝试过了
只需将.c文件和.h文件放在cpp文件夹中→可以使用SQLite3,但是没有这样的功能json
答案 0 :(得分:2)
您需要libsqlite3,而不是可执行文件,因此不需要编译 shell.c 。有一个包含{strong> Android.mk 的GitHub project为您创建了这样的库,您可能会发现static library在JNI项目中更易于使用。
答案 1 :(得分:1)
如果您在AndroidStudio中开发NDK,我发现了如何在使用CMakeLists的环境中添加Flag来使用JSON1。
首先,下载sqlite-amalgamation-(您想要的版本).zip
并解压缩.zip文件
https://www.sqlite.org/download.html(下载链接)
将获得的4个文件放在项目的cpp目录中的所需路径上。
The path to the file that I used
然后将代码添加到CMakeList.txt。
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp
src/main/cpp/sqlite/shell.c
src/main/cpp/sqlite/sqlite3.c
src/main/cpp/sqlite/sqlite3.h
src/main/cpp/sqlite/sqlite3ext.h
)
....
add_definitions( -DSQLITE_ENABLE_JSON1 )
现在您可以使用SQLite3的JSON1函数。
const char * create_sql = "create table user(name, phone)";
const char * insert_sql = "insert into user (name, phone) values(\"oz\", json('{\"cell\":\"+491765\", \"home\":\"+498973\"}'))";
const char * select_sql = "select json_extract(user.phone, '$.cell') from user";