我正在制作Android应用程序,其中我在一个开放助手类中制作了5个表。我正在我的架构中使用DELETE CASCADE时遇到问题。我知道ON DELETE CASCADE的概念。在这里,我复制了两张桌子中的一张。
private static final String MEDICINE_TABLE = "create table medicine_details (_mid integer primary key autoincrement, "
+ " medicine_name text, type text, take_with text, m_did integer not null,"
+ "FOREIGN KEY(m_did) REFERENCES doctor_details(did)"
+ " ON DELETE CASCADE" + ");";
private static final String SCH_TAB = "CREATE TABLE schedule_details(sid INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "medi_stdate TEXT,medi_end_date TEXT,time_sch TEXT,rept TEXT, alarm_id NUMBER, "
+ "u_id integer not null, doc_id integer not null, mid integer not null, "
+ "FOREIGN KEY(u_id) REFERENCES member_details(_id)"
+ " ON DELETE CASCADE,"
+ "FOREIGN KEY(doc_id) REFERENCES doctor_details(did)"
+ " ON DELETE CASCADE,"
+ "FOREIGN KEY(mid) REFERENCES medicine_details(_mid)"
+ " ON DELETE CASCADE" + ");";
每当我尝试从药物中删除时,它仅从药物表中删除,而不是从计划表中删除。这里的医学表是主表,而日程表是子表。
答案 0 :(得分:4)
您必须覆盖方法open()
,如以下示例所示:
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
最好的问候
答案 1 :(得分:1)
在您的数据库open()
方法中添加以下代码
mDb.execSQL("PRAGMA foreign_keys=ON;");