Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在sqlite中删除外键?_Android_Sqlite - Fatal编程技术网

Android 如何在sqlite中删除外键?

Android 如何在sqlite中删除外键?,android,sqlite,Android,Sqlite,我正在使用SQLite数据库。我有一个表,其中包含其他两个表的主键作为外键;我想删除其中一个。以下是表格的代码: protected static final String Item_places=(" CREATE TABLE " + Item_place + "(" + place_id + " INTEGER ," + Item_id + " INTEGER ," + "FOREIGN KEY("+place_id+ ") REFERENCES "

我正在使用SQLite数据库。我有一个表,其中包含其他两个表的主键作为外键;我想删除其中一个。以下是表格的代码:

protected static final String Item_places=(" CREATE TABLE " 
    + Item_place + "(" 
    + place_id + " INTEGER ," 
    + Item_id + " INTEGER  ," 
    + "FOREIGN KEY("+place_id+ ") REFERENCES " + PlaceTable + "("+ PlaceID+ " ) ON DELETE CASCADE" 
    + "FOREIGN KEY("+Item_id+ ") REFERENCES "+ contentTable+ "("+contentID+"));"); 

您可能需要一个
ALTER TABLE DROP CONSTRAINT
命令,但是,有关解决方法,请参阅。

这是一个老问题,但最好提供更新的答案

由于API 16(又名Android 4.1),因此可以使用
SQLiteDatabase\setForeignKeyConstraintEnabled(布尔启用)
打开FK约束

作为:

设置是否为数据库启用外键约束

默认情况下,数据库不强制外键约束。 此方法允许应用程序启用外键约束。 每次打开数据库时都必须调用它,以确保 已为会话启用外键约束

要实现此功能,请在自定义的
SQLiteOpenHelper
中使用以下代码:

@Override
public void onConfigure(SQLiteDatabase db) {
    // Enable FK constraints.
    db.setForeignKeyConstraintsEnabled(true);
    super.onConfigure(db);
}