Android SQLite and huge data sets

前端 未结 3 450
抹茶落季
抹茶落季 2020-11-28 06:39

We are creating an app for a client that has hundreds of megabytes of HTML in SQLite databases. We have implemented a way to query this data and scroll through it all in a r

3条回答
  •  一个人的身影
    2020-11-28 06:53

    If you really need that you can also split your data and read chunks like this:

       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();
    

    It's working under 2 s for 5k rows if you have indexed table.

    Thanks, Arkde

提交回复
热议问题