2015-08-07 11 views
5

come modificare questa logica per lavorare con più di 170 righe.SQLite Android si è bloccato dopo 170 righe

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

Desidero selezionare tutte le righe per stringa di input. E 'il lavoro logica perfettamente con 150 righe, ma dopo 160 lavoro lento e crash su 170 righe

+1

Perché non hai usato una clausola WHERE nella tua richiesta? –

+0

Provo con Cursore cursor = db.rawQuery ("SELECT * FROM" + TABLE_TESTS + "WHERE name =" + str, null); ma si è schiantato –

+0

Qual è l'incidente che stai vedendo? Dalla descrizione "dopo 160 lavori lentamente e crash su 170 righe" indicherebbe che l'applicazione non risponde e in quel caso il problema è probabilmente altrove, o solo che stai chiamando il codice del database sul thread dell'interfaccia utente. – laalto

risposta

4

come modificare questa logica per lavorare con più di 170 righe?

// 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 Grazie! Il tuo lavoro, ovviamente, lentamente ma funziona. (2 secondi/150 e 13 sec/1500 righe) –

+1

@AleksandarKrasimirov, questo è bello sapere. Dal momento che sei nuovo in questo sito. Lascia che ti guidi come accettare una risposta utile/giusta. Basta selezionare il segno di spunta verde oltre alla risposta. – Kedarnath

+0

Grazie :) ho fatto tic tac –

2

CURA: mispelled METODO

Usa

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

come la struttura di query, e quindi tenere traccia dell'ultima riga id come questo`

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

ogni volta che il ciclo termina, basta interrogare nuovamente il tuo db; le righe verranno recuperate dalla successiva che ti serve, poiché la variabile last_id varia dinamicamente in base ai tuoi progressi.

+0

Nota che, non conoscendo come hai chiamato il tuo campo ID intero, ho usato la variabile fantasma your_id; dovresti sostituirlo con il nome effettivo della colonna nel tuo DB. – StG

+0

Mi dà Non è possibile risolvere il metodo valueof (java.lang.String) e perché si esegue questo "LIMIT 150"? –

+0

Qual è il nome della seconda colonna? – Kedarnath

1

potrebbe provare a utilizzare lo stesso codice in modo diverso in modo seguente

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

Un altro approccio potrebbe essere quello di utilizzare LIMIT e OFFSET per ottenere i dati in parti per migliorare le prestazioni

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