2017-09-20 1 views
0

Ma requête dans android sqlite est donnée ci-dessousgroupe android sqlite erreur de requête: android.database.CursorIndexOutOfBoundsException:

MON sujet Nom de la colonne, correct_answer et total_mark, test_id
MON nom de la base no_question_list

Ma requête donnée ici

  String query = "SELECT topic,SUM(correct_answer),SUM(total_mark) from no_question_list Where test_id='" + test_id + "'" + "group by topic,correct_answer,total_mark"; 
      Cursor c = database.rawQuery(query , null); 

      if (c != null) { 
       if (c.moveToFirst()) { 
       do { 
        topic= c.getString(0); 
       } 
       while (c.moveToNext()); 
      } 
     } 

quand j'exécuter la requête aucune erreur lors de l'obtention d'erreur de la valeur du curseur

                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stepone.netquestion/com.stepone.netquestion.ResultActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 

et j'ai essayé cette partie du curseur, il doest exécuter

    if (c != null) { 
       if (c.moveToFirst()) { 
       do { 
        String topic =c.getString(c.getColumnIndex("topic"));      
        } 
       while (c.moveToNext()); 
      } 

merci pour la réponse à l'avance

Répondre

0

Je ne pense pas que vous avez besoin de ce test si (c! = Null) essayez ce code

public String getCol_MPW(){ 

    db = getReadableDatabase(); 
    String q = "SELECT * FROM masterPW"; 
    Cursor cursor = db.rawQuery(q,null); 

    if(cursor.moveToFirst()){ 
     int i = cursor.getInt(cursor.getColumnIndex(Col_IDI)); 
     str = cursor.getString(cursor.getColumnIndex(Col_MPW)); 
    } 
    cursor.close(); 
    db.close(); 

    return str; 
    // This routine called from MainActivity determine's if a 
    // password has been entered in the db table named "TABLE_PW" 
    // See onLoad() method in MainActivity 
}