-1

This is my code, the Cursor cursor= db.rawQuery is the reason. My app keep crashing

please tell me what's wrong:-

public QuestionController(Context context) {
    dbHelper= new DBHelper(context);
}

//Lấy danh sách câu hỏi
public ArrayList<Question> getQuestion(int num_exam, String subject){
    ArrayList<Question> lsData= new ArrayList<Question>();
    SQLiteDatabase db= dbHelper.getReadableDatabase();

    Cursor cursor= db.rawQuery("SELECT * FROM tracnghiem WHERE num_exam = '"+ num_exam+"' AND subject='"+subject+"' ORDER BY random()",null);
    cursor.moveToFirst();
    do {
        Question item;
        item= new Question(cursor.getInt(0), cursor.getString(1),cursor.getString(2),cursor.getString(3),
                cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getInt(7),cursor.getString(8),cursor.getString(9),"");
        lsData.add(item);
    }while (cursor.moveToNext());
    cursor.close();
    return lsData;
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134

4 Answers4

0

Not Sure But You Can Try these,

db.rawQuery("SELECT * FROM tracnghiem WHERE num_exam = ? AND subject=? ORDER BY random()", new String[] {num_exam,subject});
Samira
  • 71
  • 12
0

Not that it is definitely the issue, but you should not assume that the Cursor is not empty by not checking the result of the moveToFirst method. That is moveToFist will return false if the move cannot be made e.g. if the Cursor is empty.

If the cursor is empty the moveToFist will not fail BUT an attempt to retrieve data via a get???? method will then fail with a row index error as you are attempting to access row 0 of the cursor when there isn't a row 0.

Again not that it is definitely the error you are experiencing. However you should replace :-

cursor.moveToFirst();
do {
    Question item;
    item= new Question(cursor.getInt(0), cursor.getString(1),cursor.getString(2),cursor.getString(3),
            cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getInt(7),cursor.getString(8),cursor.getString(9),"");
    lsData.add(item);
}while (cursor.moveToNext());
cursor.close();

with something like (this will loop through the cursor if it is not empty, and not fail if the cursor is empty) :-

while (cursor.moveToNext) {
    Question item;
    item= new Question(cursor.getInt(0), cursor.getString(1),cursor.getString(2),cursor.getString(3),
            cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getInt(7),cursor.getString(8),cursor.getString(9),"");
    lsData.add(item);
}
cursor.close();

Noting that lsData will have no elements should the Cursor itself be empty. You could add something, after cursor.close() and before return lsData along the lines of :-

if (lsData.size) < 1) {
    lsData.add(new Question(0,"Empty Cursor","","","","","",0,"".""."");
}

It could then be easy to see if the Cursor was empty.

MikeT
  • 51,415
  • 16
  • 49
  • 68
0

I think problem is in this line , Before line cursor.moveToFirst(); check one condition.

if(cursor!=null && cursor.getCount() > 0){
   if(cursor.moveToFirst()){
       //your process for fetching row.
   }
}
Dharmishtha
  • 1,313
  • 10
  • 21
  • `cursor!=null` is needless as a cursor, at least from SQLiteDatabase methods, is never null. – MikeT Dec 07 '17 at 06:43
0

In your case, there can be only 3 leaks for crashing app,

  • Established connection with DB is not proper.
  • Wrong query (table or fields in table not exist in DB).
  • Your cursor returns 0 rows.
Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27