Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite crashed after 170 row

Tags:

android

sqlite

how to change this logic to work with more than 170 rows.

// Getting All test 
public List<Test> getAllTests(String str) {
    List<Test> testList = new ArrayList<Test>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_TESTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

   // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            //select rows by input string   
            if(cursor.getString(1).equals(str)){
                Test test = new Test();
                test.setId(Integer.parseInt(cursor.getString(0)));
                test.setTest(cursor.getString(1));
                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;
}

I want to select all rows by input string. It logic work perfectly with 150 rows but after 160 work slowly and crash on 170 rows

like image 539
Aleksandar Krasimirov Avatar asked Aug 07 '15 08:08

Aleksandar Krasimirov


1 Answers

how to change this logic to work with more than 170 rows?

// Getting All test 
public List<Test> getAllTests(String str) {
    List<Test> testList = new ArrayList<Test>();
    // Select All Query

    //String selectQuery = "SELECT  * FROM " + TABLE_TESTS;
String selectQuery = "SELECT id,result FROM " + TABLE_TESTS + " where name ='" + str + "'";
    // Now you are saving memory of one column. 

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor 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)){
            // No need for if Codition any more
                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;
}
like image 71
Lucifer Avatar answered Sep 17 '22 20:09

Lucifer