使用 SQLite 的 Android 中的外键约束?在删除级联 [英] Foreign key constraints in Android using SQLite? on Delete cascade
问题描述
我有两个表:tracks 和waypoints,一个track 可以有多个waypoint,但是一个waypoint 只分配给1 个track.
I have two tables: tracks and waypoints, a track can have many waypoints, but a waypoint is assigned to only 1 track.
在路径点表中,我有一个名为trackidfk"的列,它会在制作曲目后插入 track_ID,但是我没有在该列上设置外键约束.
In the way points table I have a column called "trackidfk" which inserts the track_ID once a track is made, however I have not setup Foreign Key constraints on this column.
当我删除轨迹时,我想删除指定的航点,这可能吗?.我阅读了有关使用触发器的信息,但我认为 Android 不支持它们.
When I delete a track I want to delete the assigned waypoints, is this possible?. I read about using Triggers but I don't think they are supported in Android.
要创建航点表:
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE " + TABLE_NAME
+ " ("
+ _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ LONGITUDE + " INTEGER,"
+ LATITUDE + " INTEGER,"
+ TIME + " INTEGER,"
+ TRACK_ID_FK + " INTEGER"
+ " );"
);
...
}
推荐答案
支持删除级联的外键约束,但您需要启用它们.
我刚刚将以下内容添加到我的 SQLOpenHelper 中,这似乎可以解决问题.
Foreign key constraints with on delete cascade are supported, but you need to enable them.
I just added the following to my SQLOpenHelper, which seems to do the trick.
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
我如下声明了我的引用列.
I declared my referencing column as follows.
mailbox_id INTEGER REFERENCES mailboxes ON DELETE CASCADE
这篇关于使用 SQLite 的 Android 中的外键约束?在删除级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!