2

我正在制作 android 应用程序,其中我在一个开放的助手类中制作了 5 个表。当我的架构中有 ON 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" + ");";

每当我尝试从药物中删除时,它只会从药物表中删除,而不是从时间表表中删除。这里的医学表是主表,日程表是子表。

4

2 回答 2

4

您必须重写该方法open(),如以下示例所示:

    @Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}

此致

于 2015-02-01T14:54:18.200 回答
1

在您的数据库open()方法中添加以下代码
mDb.execSQL("PRAGMA foreign_keys=ON;");

于 2013-04-18T17:24:54.310 回答