Search code examples
androidandroid-cursordatabase-cursor

Android how to query huge database in android (cursor size is limited to 1MB)


I'm working to develop an application that has to query at some time, a database with over 4k rows, and each row has 90 fields (Strings). The problem is that if I select * from database, my cursor gets really big (over 4MB). And the cursor in android is limited to 1MB.

How can I solve this, or what's the most elegant method to workaround this? It is possible to split database in smaller chunks and query them out?


Solution

  • I found a way to handle this and I want to share with all who need it.

       int limit = 0;
       while (limit + 100 < numberOfRows) {
           //Compose the statement
           String statement = "SELECT * FROM Table ORDER someField LIMIT '"+ limit+"', 100";
           //Execute the query
           Cursor cursor = myDataBase.rawQuery(statement, null);
           while (cursor.moveToNext()) {
               Product product = new Product();
               product.setAllValuesFromCursor(cursor);
               productsArrayList.add(product);
          }
          cursor.close();
          limit += 100;
     }
    
     //Compose the statement
     String statement = "SELECT * FROM Table ORDER someField LIMIT '"+  (numberOfRows - limit)+"', 100";
     //Execute the query
     Cursor cursor = myDataBase.rawQuery(statement, null);
    
     while (cursor.moveToNext()) {
         Product product = new Product();
         product.setAllValuesFromCursor(cursor);
         productsArrayList.add(product);
     }
     cursor.close();
    

    The main idea is to split your data, so you can use the cursor as it should be used. It's working under 2 s for 5k rows if you have indexed table.

    Thanks, Arkde