jeudi 30 juillet 2015

ANDROID: attempt to re-open an already-closed object?

I am trying a delete data from my table(s) however it seems that I am getting an error. So basically in my database contains two tables that are connected through an "exercise ID". So to delete data from my database, I would need to first get an exercise ID from the row I want deleted, then, use that as a reference against my second table and delete everything that is associated with it. So heres what I have so far that does not seem to want to work...

public void deleteRowFromDatabase(String exerciseName, String bodyPart) {

    SQLiteDatabase db = myDBHelper.getWritableDatabase();

    int exerciseID = getExerciseID(exerciseName, bodyPart);
    String query = "SELECT " + myDBHelper.COLUMN_EXERCISENAME_ID + " FROM " + myDBHelper.TABLE_EXERCISES_VALUE +
                    " WHERE " + myDBHelper.COLUMN_EXERCISENAME_ID + "=\"" + exerciseID + "\";";

    Cursor c = db.rawQuery(query, null);

    while (c.getCount()>0){
        c.moveToFirst();
        db.execSQL("DELETE FROM " + myDBHelper.TABLE_EXERCISES_VALUE + " WHERE " + myDBHelper.COLUMN_EXERCISENAME_ID +
                    "=\"" + exerciseID + "\";");
        c.moveToNext();
    }

    db.execSQL("DELETE FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " + myDBHelper.COLUMN_BODYPARTNAME +
                "=\"" + bodyPart + "\"" + " AND " + myDBHelper.COLUMN_EXERCISENAME + "=\"" + exerciseName + "\";");

    c.close();
    db.close();
}

Aucun commentaire:

Enregistrer un commentaire