How can I query an SQLite database from my Android Processing Sketch?

edited August 2015 in Android Mode

I cannot figure out how to do it using Processing 3. Does anyone know how?

Answers

  • You need to use ketai library. Does this example help? http://ketai.org/examples/sqlite/

  • Thanks! That helps very much, but I still have an issue. Since I am using a SQLite database stored elsewhere on my device, I am trying to use this example instead, but I am having an issue that I do not understand.

    Click on the picture to see a larger version of it.

    Ketai Library SQLite

  • I'm fairly certain that replacing this with getActivity() will do the trick:

    KetaiSQLite.load(getActivity(), "path/to/foo", "bar");
    
  • It appears that the issue I was experiencing was because of a bug in the Ketai library which the developer has patched. At this time, a new official build is not available, so anyone experiencing this issue that wants to get a working copy needs to compile it on their own as I have done.

    I am experiencing a different issue now. I cannot access the database on my internal storage. Apparently, the line of code underlined in red in the above picture is searching for the database in the resources folder of the sketch and not directly on the device's internal storage as I need. What can I do about it?

  • edited August 2015 Answer ✓

    @DavidB== by default android .sqlite when created or used are stored in a folder called "data". I think that he ketai "load()" method uses the path to this folder and is not able to go elsewhere. Yet, you can easily do what you want using the android classes for sqlite database. See the code below (i have manually put the sqlite "large" on my phone, then i open it && list the tables inside for testing). Don't forget the uses permission

        import android.database.sqlite.SQLiteDatabase;
        import android.os.Environment;
        import android.database.Cursor;
    
    
    
        Context context;
    
        void setup()
        {
    
        String directory = Environment.getExternalStorageDirectory().getAbsolutePath();
        File dbFile = new File(directory+ "/large.sqlite");
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
    
        Cursor c = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);//for testing
    
        if (c.moveToFirst()) {
            while ( !c.isAfterLast() ) {
                println( "Table Name=> "+c.getString(0));
                c.moveToNext();
            }
        }
    
    
        };
    
  • Thank you, although I was able to get what I wanted done by writing a custom library for my sketch that allows for querying of a SQLite database stored directly on the device's internal storage. This is nice because since the library was written specifically for my project, I do not need to write extra code for connecting to the database in the sketch, as it is all handled directly in the library itself.

Sign In or Register to comment.