在sqlite android中使用common列删除两个表并且没有引用

时间:2016-03-22 15:22:43

标签: android sqlite

我有两个表Class和Students,我希望,当删除表Class时,通过使用类似下面的内容删除具有相同Class id的学生..

public Integer deleteClass (Integer id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete("Class","_id = ? ",new String[] { Integer.toString(id) });
}

使用类似以下的模式

private static final String CREATE_STUDENTS_TABLE = "CREATE TABLE "
    + STUDENTS_TABLE + "(" + TABLE_STUDENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + TABLE_STUDENT_NAME + " TEXT, "
    + TABLE_STUDENT_AGE + " TEXT, "
    + TABLE_STUDENT_CLASS_ID + " INTEGER, "
    + TABLE_STUDENT_AREA + " TEXT" + ")";

private static final String CREATE_CLASS_TABLE = "CREATE TABLE "
    + CLASS_TABLE + "(" + TABLE_CLASS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + TABLE_CLASS_NAME + " TEXT, "
    + TABLE_CLASS_CAPACITY + " TEXT, "
    + TABLE_CLASS_POSITION + " TEXT"+ ")";

1 个答案:

答案 0 :(得分:0)

在从Class删除之前,请从同一个班级中删除所有Students

public Integer deleteClass (Integer id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(STUDENTS_TABLE, TABLE_STUDENT_CLASS_ID + " = ? ",new String[] { Integer.toString(id) });
    return db.delete(CLASS_TABLE, TABLE_CLASS_ID + " = ? ",new String[] { Integer.toString(id) });
}

注意:我正在考虑表格Students的列名class_id是从表格Class引用的。