android NDK中sqlite3數據庫的使用

工做須要使用數據庫進行大量的插入工做,故此想經過使用c進行這部分工做,通過查資料,發現一篇文章比較實用:
 方法1.使用源碼sqlite3.h,sqlite3.c進行編譯。
 
 方法2.使用android編譯好獲得得libsqlite.so庫
    具體方法:
    1) 使用adb命令 adb pull /system/lib/libsqlite.so 獲得libsqlite.so庫(在當前用戶的根目錄下)。

    2)把獲得libsqlite.so拷貝到$NDK/platforms/android-3/arch-arm/usr/lib 目錄下(建議拷貝到platforms全部子目錄的相應位置)

    3)把sqlite3.h頭文件拷貝到$NDK/platforms/android-3/arch-arm/usr/include目錄下(同上),頭文件可再在此下載

    4)在android.mk文件中加入語句 LOCAL_LDLIBS := -lsqlite (注意若是須要連接編譯多個動態庫時,在該語句後面接這加動態庫如:LOCAL_LDLIBS := -lsqlite  -llog)

   5)如下使用代碼:
    #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
     #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))
 
   extern "C" {
    JNIEXPORT
    void JNICALL Java_com_sinde_nativedb_NativeDBHelper_insertWithTableName(JNIEnv* env,jobject obj,jstring dbPath,jstring          f ilePath,jstring tableName);
   }
   JNIEXPORT
void JNICALL Java_com_sinde_nativedb_NativeDBHelper_insertWithTableName(JNIEnv* env,jobject obj,jstring dbPath,jstring filePath,jstring tableName){
    sqlite3 *db;
    char *pErrMsg = 0;
    char* dbP = jstringTostring(env,dbPath);
    char* fP = jstringTostring(env,filePath);
    char* tName = jstringTostring(env,tableName);
    int openState = sqlite3_open(dbP,&db);
    if(openState != SQLITE_OK){
        return;
    }
    FILE *f;
    if((f = fopen(fP,"r")) == NULL){
        return;
    }
    char line[4096];
    char sql[6134];
    char head[1024];
    memset(line,0,4096);
    memset(sql,0,6134);
    memset(head,0,1024);
    readLine(f,head);
    LOGW("line:%s",head);
    int size = 0;
    while(!feof(f)){
        readLine(f,line);
        sprintf(sql,"insert into %s (%s) values (%s)",tName,head,line);
        if(size==0){
            sqlite3_exec(db, "BEGIN;", 0, 0, &pErrMsg);
        }
//        LOGW("sql:%s",sql);
        sqlite3_exec(db,sql,0,0,&pErrMsg);
        if(size == 5000){
            size = 0;
            sqlite3_exec(db, "COMMIT;", 0, 0, &pErrMsg);
            LOGW("insert 5000");
        }else{
            size++;
        }
        memset(line,0,4096);
        memset(sql,0,6134);
        sqlite3_free(pErrMsg);
    }
    if(size != 0){
        sqlite3_exec(db, "COMMIT;", 0, 0, &pErrMsg);
        sqlite3_free(pErrMsg);
    }
    LOGW("insert over");
    fclose(f);
    sqlite3_close(db);
}
//讀文件的一行
char* readLine(FILE* f,char* line){
    char* start = line;
    char temp;
    while(((temp = fgetc(f)) != '\n') && !feof(f)){
        *start++ = temp;
    }
    return line;
}
//jstring轉char*
char* jstringTostring(JNIEnv* env, jstring jstr)
{
       char* rtn = NULL;
       jclass clsstring = env->FindClass("java/lang/String");
       jstring strencode = env->NewStringUTF("utf-8");
       jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
       jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
       jsize alen = env->GetArrayLength(barr);
       jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
       if (alen > 0)
       {
                 rtn = (char*)malloc(alen + 1);
                 memcpy(rtn, ba, alen);
                 rtn[alen] = 0;
       }
       env->ReleaseByteArrayElements(barr, ba, 0);
       return rtn;
}
html

相關文章
相關標籤/搜索