对不起,我首先缺乏英语。
我编译它是因为我需要 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