gpt4 book ai didi

android - SqliteConstraintException Android

转载 作者:行者123 更新时间:2023-12-03 18:18:07 26 4
gpt4 key购买 nike

我有一个SqliteConstraintException问题。
我的数据库就是这样

private static final String CREATE_TABLE_1 =
"create table table_1 (uid integer primary key autoincrement, "
+ table_1.c1 + " TEXT,"
+ table_1.c2 + " integer,"
+ table_1.c3 + " TEXT,"
+ table_1.c4 + " TEXT,"
+ table_1.c5 + " integer);";

private static final String CREATE_TABLE_2 =
"create table table_2 (uid integer primary key autoincrement, "
+ table_2.c1 + " TEXT,"
+ table_2.c2 + " integer,"
+ table_2.c3 + " TEXT);";

private static final String CREATE_TABLE_3 =
"create table table_3 (uid integer primary key autoincrement, "
+ "uid_table_1 integer, "
+ "uid_table_2 integer, "

+ "FOREIGN KEY(uid_table_1) REFERENCES table_1(uid) ON DELETE CASCADE, "
+ "FOREIGN KEY(uid_table_2) REFERENCES table_2(uid) ON DELETE CASCADE);";

private static final String CREATE_TABLE_4 =
"create table table_4 (uid integer primary key autoincrement, "
+ "uid_table_2 integer,"
+ "FOREIGN KEY(uid_table_2) REFERENCES table_2(uid) ON DELETE CASCADE);";


我在我的SQLiteOpenHelper中使用以启用外键约束

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


但是,当我想在table_2的行上删除时,我得到SqliteConstraintException。
你能帮我吗 ?

谢谢

最佳答案

我看起来您的大多数表都相互引用。如果要删除表2中用作另一表中的外键的行,则需要删除引用该行的记录。因此,请删除表3和4中的记录,这些记录具有要在2中尝试删除的行的外键引用,然后对该行调用delete。

关于android - SqliteConstraintException Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13053787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com