mardi 8 septembre 2015

The CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

I have implemented a SQL statement that returns a Boolean value of true is the Username and Date exist in the Table. Below is my implementation. I had also included the current error that is displayed in the Logcat

public boolean flagExists(String Username, String Date) {
SQLiteDatabase db = getReadableDatabase();
String selectString = "SELECT * FROM " + TABLE_FLAGS + " WHERE " + COLUMN_FLAGS_USERNAME + " = '" + Username + " ' " + " AND " + COLUMN_FLAGS_DATE + "= '" + Date + "'";

// Add the String you are searching by here.
// Put it in an array to avoid an unrecognized token error
Cursor cursor = db.rawQuery(selectString, new String[]{Username,Date});

boolean hasObject = false;
if (cursor.moveToFirst()) {
    hasObject = true;

    //region if you had multiple records to check for, use this region.
    int count = 0;
    while (cursor.moveToNext()) {
        count++;
    }
    //here, count is records found
    Log.d(TAG, String.format("%d records found", count));

    //endregion

}
cursor.close();          // Dont forget to close your cursor
db.close();              //AND your Database!
return hasObject;

}

rocess: com.example.s210121629.myhealth, PID: 10953
    android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:86)
            at com.example.s210121629.myhealth.Database.DBHelper.getPerson(DBHelper.java:400)
            at com.example.s210121629.myhealth.dashboard.DashboardActivity.decisionAlgorithm(DashboardActivity.java:347)
            at com.example.s210121629.myhealth.dashboard.DashboardActivity.reloadlist(DashboardActivity.java:327)
            at com.example.s210121629.myhealth.dashboard.DashboardActivity$3.onFinish(DashboardActivity.java:252)
            at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
            at android.os.Handler.dispatchMessage(Handler.java:102)

Aucun commentaire:

Enregistrer un commentaire