mardi 7 juillet 2015

Query Broken after adding another where clause

I literally added the second where clause in the query and now it says: "android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0"

I just added a where clause to narrow down the results and now it wants to say the cursor has nothing in it. If I remove the second where clause, it runs fine and returns everything correctly. Why would this crash my application when I add a where clause?

public ArrayList<Athlete> getAllMaleAthletes(){
    ArrayList<Athlete> allAthletes = new ArrayList<Athlete>();
    SQLiteDatabase db = this.getReadableDatabase();

    // Gets number of athletes in Athletes table
    Cursor cursor = db.rawQuery("SELECT COUNT(*) AS numAthletes FROM " + TABLE_ATHLETES, null);
    cursor.moveToFirst();
    int count = cursor.getInt(cursor.getColumnIndex("numAthletes"));
    cursor.close();
    Log.e("", "numAthletes in Database: " + count);

    // Joins tables together to get each individual athlete (could have many rows if athlete has many events)
    for(int i = 1; i < count + 1; i++){
        Athlete athlete = new Athlete();
        Cursor cursorTemp = db.rawQuery("SELECT " + TABLE_ATHLETES + "." + COL_ATHLETE_ID + ", " +
                TABLE_ATHLETES + "." + COL_FIRST_NAME + ", " + TABLE_ATHLETES + "." + COL_LAST_NAME + ", " +
                TABLE_ATHLETES + "." + COL_GENDER + ", " + TABLE_ATHLETES + "." + COL_AGE + ", " +
                TABLE_ATHLETES + "." + COL_GRADE + ", " + TABLE_ATHLETES + "." + COL_IMAGE + ", " +
                TABLE_EVENTS + "." + COL_EVENT + ", " + TABLE_ATHLETES_EVENTS + "." + COL_TIER + ", " +
                TABLE_ATHLETES_EVENTS + "." + COL_PERSONAL_RECORD + ", " + TABLE_ATHLETES_EVENTS + "." + COL_PR_DATE +

                " FROM " + TABLE_ATHLETES +
                " INNER JOIN " + TABLE_ATHLETES_EVENTS + " ON " + TABLE_ATHLETES + "." + COL_ATHLETE_ID +
                " = " + TABLE_ATHLETES_EVENTS + "." + COL_ATHLETE_ID +
                " INNER JOIN " + TABLE_EVENTS + " ON " + TABLE_ATHLETES_EVENTS + "." + COL_EVENT_ID +
                " = " + TABLE_EVENTS + "." + COL_EVENT_ID +
                " WHERE " + TABLE_ATHLETES + "." + COL_ATHLETE_ID + " = ? AND " +
                TABLE_ATHLETES + "." + COL_GENDER + " = ?", new String[] {String.valueOf(i), "Male"});
        ArrayList<Event> tempEvents = new ArrayList<Event>();
        if(cursorTemp.moveToFirst() && cursorTemp != null){
            do{
                Event event = new Event();
                event.setEvent(cursorTemp.getString(cursorTemp.getColumnIndex(COL_EVENT)));
                event.setTier(cursorTemp.getString(cursorTemp.getColumnIndex(COL_TIER)));
                event.setPersonalRecord(cursorTemp.getString(cursorTemp.getColumnIndex(COL_PERSONAL_RECORD)));
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date(cursorTemp.getLong(cursorTemp.getColumnIndex(COL_PR_DATE)));
                String dateString = simpleDateFormat.format(date);
                event.setPrDate(dateString);
                tempEvents.add(event);
            }while(cursorTemp.moveToNext());
        }
        cursorTemp.moveToFirst();
        athlete.setAthleteID(i);
        athlete.setFirstName(cursorTemp.getString(cursorTemp.getColumnIndex(COL_FIRST_NAME)));
        athlete.setLastName(cursorTemp.getString(cursorTemp.getColumnIndex(COL_LAST_NAME)));
        athlete.setGender(cursorTemp.getString(cursorTemp.getColumnIndex(COL_GENDER)));
        athlete.setAge(cursorTemp.getInt(cursorTemp.getColumnIndex(COL_AGE)));
        athlete.setGrade(cursorTemp.getString(cursorTemp.getColumnIndex(COL_GRADE)));
        byte[] image = cursorTemp.getBlob(cursorTemp.getColumnIndex(COL_IMAGE));
        if(image != null) {
            Bitmap profilePic = BitmapFactory.decodeByteArray(image, 0, image.length);
            athlete.setImage(profilePic);
        }
        athlete.setEvents(tempEvents);

        allAthletes.add(athlete);
        cursorTemp.close();
    }
    return allAthletes;
}

Aucun commentaire:

Enregistrer un commentaire