2015-08-07 7 views
5

jak zmienić tę logikę, aby działała z więcej niż 170 rzędami.Android SQLite zawieszał się po 170 wierszach

// 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; 
} 

Chcę zaznaczyć wszystkie wiersze według ciągu wejściowego. To dzieło logika doskonale z 150 wierszy, ale po 160 prac powoli i katastrofy na 170 wierszy

+1

Dlaczego don czy w swojej prośbie używasz klauzuli WHERE? –

+0

Próbuję z kursorem kursora = db.rawQuery ("SELECT * FROM" + TABLE_TESTS + "WHERE name =" + str, null); ale rozbił się –

+0

Jaka jest awaria, którą widzisz? Z opisu "po 160 pracy wolno i awarii na 170 wierszy" zgadłaby aplikacja nie odpowiada, aw tym przypadku problem jest prawdopodobnie gdzie indziej, lub po prostu, że dzwonisz kod bazy danych w wątku UI. – laalto

Odpowiedz

4

jak zmienić tę logikę do pracy z więcej niż 170 rzędami?

// 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; 
} 
+0

@ Kedarnath Dzięki! Twoja praca, oczywiście powoli, ale działa. (2 sekundy/150 rzutów i 13 sekund/1500 wierszy) –

+1

@AleksandarKrasimirov, to dobrze wiedzieć. Ponieważ jesteś nowy na tej stronie. Pozwól, że poprowadzę cię, jak zaakceptować użyteczną/prawą odpowiedź. Po prostu zaznacz pole wyboru obok zielonego znaku podziałki. – Kedarnath

+0

Dzięki :) i zaznaczone –

2

edycja: mispelled METODY

Zastosowanie

String selectQuery = "SELECT * FROM " + TABLE_TESTS + " WHERE " + your_id + " > " + String.valueOf(last_id) + " LIMIT 150"; 

jako struktury kwerendy, a następnie śledzić ostatnim rzędzie id tak jak ten "

int last_id; 
do { 
     //select rows by input string 
     if(cursor.getString(1).equals(str)){ 
      Test test = new Test(); 
      last_id = Integer.parseInt(cursor.getString(0)); 
      test.setId(last_id); 
      ... 
     } 
    } while (cursor.moveToNext()); 

za każdym razem, gdy pętla się kończy, po prostu zapytaj ponownie o swoją db; wiersze zostaną pobrane z następnego, którego potrzebujesz, ponieważ zmienna last_id zmienia się dynamicznie zgodnie z twoimi postępami.

+0

Zauważ, że nie wiedząc, jak nazwałeś swoje pole identyfikatora całkowitoliczbowego, użyłem zmiennej duchowej twój_id; powinieneś zastąpić to faktyczną nazwą kolumny w twoim DB. – StG

+0

To daje mi Nie można rozwiązać metody valueof (java.lang.String) i dlaczego robisz to "LIMIT 150"? –

+0

Jak nazywa się twoja druga kolumna? – Kedarnath

1

można spróbować użyć tego samego kodu różnie w następujący sposób

// 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 i.e. value to replace ? /, 
    null/groupBy /, 
    null/having /, 
    null/orderBy/
); 

inne podejście mogłoby być użycie LIMIT i OFFSET, aby uzyskać dane w części w celu zwiększenia wydajności

// 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; 
}