Use of the Sqlite3 database in the Android NDK

Source: Internet
Author: User

Work needs to use the database for a lot of inserting work, so I want to use C to do this part of the work, after looking at the data, found an article is more practical:
Method 1. Compile using the source sqlite3.h,sqlite3.c.

Method 2: Compile with Android to get the libsqlite.so library
Specific methods:
1) use adb command adb pull/system/lib/libsqlite.so to get the libsqlite.so library (under the current user's root directory).

2) Copy the obtained libsqlite.so to the $ndk/platforms/android-3/arch-arm/usr/lib directory (it is recommended to copy to the corresponding location of all platforms subdirectories)

3) Copy the Sqlite3.h header file to the $ndk/platforms/android-3/arch-arm/usr/include directory (IBID.), and the header file can be downloaded here.

4) in the Android.mk file, add the statement local_ldlibs: =-lsqlite (Note that if you need to link to compile multiple dynamic libraries, then add the dynamic library after the statement such as: local_ldlibs: =-lsqlite-llog)

5) Use the following code:
#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);
}
Read a line of the file
char* readLine (file* f,char* line) {
char* start = line;
char temp;
while ((temp = fgetc (f))! = ' \ n ') &&!feof (f)) {
*start++ = temp;
}
return line;
}
Jstring Turn 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;
}

Use of the Sqlite3 database in the Android NDK

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.