如何使用Android NDK SQLite3 扩展json1

How to use Android NDK SQLite3 extension json1

不好意思我先缺英文

我编译它是因为我需要 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

您需要 libsqlite3,而不是可执行文件,因此您不需要编译 shell.c。在您的 JNI 项目中有一个 GitHub project that includes the Android.mk that creates such library for you, you will probably find the static library 更易于使用。

如果您在 AndroidStudio 中开发 NDK,我发现如何通过在使用 CMakeLists 的环境中添加一个标志来使用 JSON1。

首先,下载sqlite-amalgamation-(你要的版本).zip
并解压 .zip 文件
https://www.sqlite.org/download.html(下载 link)

将获取到的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";