You can try using the same code differently as follows
// using sql query Differently (SQliteDatabase) db.query( "TABLE_TESTS" / table name /, new String[] { "id", "result" } / columns names /, "name = ?" / where or selection /, new String[] { str } / selectionArgs ie value to replace ? /, null / groupBy /, null / having /, null / orderBy / );
Another approach might be to use LIMIT and OFFSET to retrieve data in parts to improve performance.
// using LIMIT AND OFFSET public List<Test> getAllTests(String str) { List<Test> testList = new ArrayList<Test>(); // Select All Query Integer count = 0; String countQuery = "SELECT count(id) FROM " + TABLE_TESTS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); if (cursor.moveToFirst()) { count= c.getCount(); } db.close(); int MAX_LENGTH = 150; if ( count > 0 ) { int total_length = ( count / MAX_LENGTH ) + 1; for ( int i=0; i<total_length; i++) { String selectQuery = "SELECT id,result FROM " + TABLE_TESTS + " LIMIT " + MAX_LENGTH + " OFFSET " + (i*MAX_LENGTH) ; db = this.getWritableDatabase(); cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { Test test = new Test(); // moved outside loop to prevent creating new object every time. do { //select rows by input string if(cursor.getString(1).equals(str)){ test.setId(Integer.parseInt(cursor.getString(0))); //test.setTest(cursor.getString(1)); test.setTest(str); test .setResult(Integer.parseInt(cursor.getString(2))); // Adding test to list testList.add(test); } } while (cursor.moveToNext()); } //close database db.close(); } } //return list data return testList; }
source share