ANDROID:尝试重新打开已经关闭的对象?

时间:2015-07-31 03:40:39

标签: android database sqlite

我正在尝试从我的表中删除数据,但似乎我收到了错误。所以基本上在我的数据库中包含两个通过"运动ID"连接的表。因此,要从我的数据库中删除数据,我需要首先从我想要删除的行中获取一个练习ID,然后使用它作为对我的第二个表的引用并删除与其关联的所有内容。所以我到目前为止所看到的似乎并不想工作......

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();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LOW LEVEL METHODS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
public static class MyDBHelper extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "exercises.db";
    public static final String TABLE_EXERCISES = "exercises";
    public static final String TABLE_EXERCISES_VALUE = "exercises_value";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_BODYPARTNAME = "bodypartname";
    public static final String COLUMN_EXERCISENAME = "exercisename";

    public static final String COLUMN_EXERCISENAME_ID = "exerciseid";
    public static final String COLUMN_NUMSETS = "numsets";
    public static final String COLUMN_NUMWEIGHT = "numweight";
    public static final String COLUMN_NUMREPS = "numreps";


    public MyDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        //used for debugging
       // context.deleteDatabase(DATABASE_NAME);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_EXERCISES + "(" +
                COLUMN_ID  + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_BODYPARTNAME + " TEXT NOT NULL," +
                COLUMN_EXERCISENAME + " TEXT NOT NULL" +
                ");";

        db.execSQL(query);

        query = "CREATE TABLE " + TABLE_EXERCISES_VALUE + "(" +
                COLUMN_ID  + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_EXERCISENAME_ID + " INTEGER NOT NULL," +
                COLUMN_NUMSETS + " INTEGER NOT NULL," +
                COLUMN_NUMWEIGHT + " DOUBLE NOT NULL," +
                COLUMN_NUMREPS + " INTEGER NOT NULL" +
                ");";

        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXIST " + TABLE_EXERCISES);
        db.execSQL("DROP TABLE IF EXIST " + TABLE_EXERCISES_VALUE);
        onCreate(db);
    }

}

日志数据

   07-29 02:04:44.748    9539-9539/com.example.edwardlim.workoutlogs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.edwardlim.workoutlogs, PID: 9539
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.edwardlim.workoutlogs/databases/exercises.db
        at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1312)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
        at com.example.edwardlim.workoutlogs.MyDBAdapter.deleteRowFromDatabase(MyDBAdapter.java:120)
        at com.example.edwardlim.workoutlogs.ExercisesSection$3.onClick(ExercisesSection.java:182)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

0 个答案:

没有答案