Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite R-Tree - How to install module?

http://www.sqlite.org/rtree.html says that the r*tree is "included as part of the amalgamation but is disabled by default" and to enable it "simply compile with the SQLITE_ENABLE_RTREE C-preprocessor macro defined"

Well I want to use R-trees in my android app, but clearly SQLite is all pre-installed etc. Is there a way to enable it on a user's phone/device?

Alternatively, is it possible to use the NDK and the freely available source code for SQLite?

like image 399
James Coote Avatar asked May 26 '11 00:05

James Coote


2 Answers

June 2017

  1. Go to the: download page and grab sqlite-android-xxxxx.aar
  2. Rename it to .aar instead of .zip
  3. Create a simple project with Android Studio (no need for C++ support)
  4. In the project tree right click app / new / Module select Import .JAR/.AAR Package
  5. Select the renamed file, click finish
  6. In Android Studio go to menu: File / Project structure. Click app (under Modules), select the Dependencies Tab, Add module dependency SQLite-android-xxxxx
  7. Before using any SQLite functions, call: System.loadLibrary("sqliteX");
  8. Replace SQlite imports to org.sqlite.database.sqlite.xxxxxx
  9. In onCreate you can make a quick test with a memory database:

    System.loadLibrary("sqliteX");
    
    // get the SQLite version
    String query = "select sqlite_version() AS sqlite_version";
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(":memory:", null);
    Cursor cursor = db.rawQuery(query, null);
    String sqliteVersion = "";
    if (cursor.moveToNext()) {
        sqliteVersion = cursor.getString(0);
    }
    
    // do some R*Tree things (this will fail for the standard SQLite)
    db.execSQL("CREATE VIRTUAL TABLE demo_index USING rtree(id, minX, maxX, minY, maxY);");
    db.execSQL("INSERT INTO demo_index VALUES(1,-80.7749, -80.7747, 35.3776, 35.3778);");
    db.execSQL("INSERT INTO demo_index VALUES(2,-81.0, -79.6, 35.0, 36.2);");
    
    cursor = db.rawQuery("SELECT id FROM demo_index WHERE minX>=-81.08 AND maxX<=-80.58 AND minY>=35.00  AND maxY<=35.44;", null);
    
    int id = -1;
    if (cursor.moveToFirst()) {
        do {
            id = cursor.getInt(0);
        } while (cursor.moveToNext());
    }
    db.close();
    

The links (for all of the above):

  • SQLite Android Bindings
  • Installation
  • Very Simple App
  • Application Programming
  • The SQLite R*Tree Module
like image 62
A.J.Bauer Avatar answered Oct 06 '22 22:10

A.J.Bauer


You can absolutely compile your own version of SQLite. We do this in order to enable the encryption/codec modules from wxSQLite. Take a look at the SQLite source in the Android Git repository. Basically it's as easy and creating a Android.mk with the options (such as SQLITE_ENABLE_RTREE) you'd like enabled. Of course, this will give you a native library. In order to use it you'll need to access it from the NDK or create a wrapper (again, you can look at the Android repository and Java/JNI wrappers to SQLite)

like image 25
NuSkooler Avatar answered Oct 06 '22 22:10

NuSkooler