Android中使用SQLite的外键约束?在删除级联
我有两张表:tracks 和 waypoints,一个track 可以有很多waypoints,但是一个waypoint 只能分配给一个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"
+ " );"
);
...
}
推荐答案
支持 on delete cascade 的外键约束,但您需要启用它们.
我刚刚在我的 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
相关文章