Android SQLite:约束失败错误代码19

时间:2021-10-21 05:28:19

I have execute the following sql:

我执行了以下sql:

update record set out_payment=4 where out_payment=4;

更新记录集out_payment = 4其中out_payment = 4;

the out_payment of record is defined as Integer and reference the Payment(id)

记录的out_payment定义为Integer并引用Payment(id)

I can ensure the 4 was the one of the ID of the payment table;

我可以确保4是付款表的ID之一;

but I still got the constrained failed....

但我仍然受到约束失败....

07-31 10:20:36.014: ERROR/Database(19085): Error updating out_payment=4 using UPDATE record_table SET out_payment=? WHERE out_payment = 4
07-31 10:20:45.964: ERROR/AndroidRuntime(19085): FATAL EXCEPTION: main
07-31 10:20:45.964: ERROR/AndroidRuntime(19085): android.database.sqlite.SQLiteConstraintException:   error code 19: constraint failed

the code is as following:

代码如下:

values.clear();
values.put(RecordSchema.ID_OUT_PAYMENT, oldid);
selection = RecordSchema.ID_OUT_PAYMENT + " = " + oldid + "";
this.db.update(Table.RECORD, values, selection, null);

the Schema is as following :

架构如下:

db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE.RECORD
+ " (" + RecordSchema.ID + " INTEGER PRIMARY KEY"
+ "," + RecordSchema.AMOUNT + " TEXT NOT NULL"
+ "," + RecordSchema.ID_CATEGORY + " INTEGER NOT NULL"
+ "," + RecordSchema.ID_SUBCATEGORY + " INTEGER"
+ "," + RecordSchema.DATE + " DATE NOT NULL"
+ "," + RecordSchema.ID_IN_PAYMENT + " INTEGER"
+ "," + RecordSchema.ID_OUT_PAYMENT + " INTEGER"

+ ",FOREIGN KEY(" + RecordSchema.ID_CATEGORY + ") REFERENCES " 
+ IsAiZanTable.CATEGORY + "(" + CategorySchema.ID + ") ON UPDATE CASCADE"

+ ",FOREIGN KEY(" + RecordSchema.ID_SUBCATEGORY + ") REFERENCES "
+ IsAiZanTable.SUBCATEGORY + "(" + SubcategorySchema.ID + ") ON UPDATE CASCADE"

+ ",FOREIGN KEY(" + RecordSchema.ID_IN_PAYMENT + ") REFERENCES "
+ IsAiZanTable.PAYMENT + "(" + PaymentSchema.ID + ") ON UPDATE CASCADE"

+ ",FOREIGN KEY(" + RecordSchema.ID_OUT_PAYMENT + ") REFERENCES "
+ IsAiZanTable.PAYMENT + "(" + PaymentSchema.ID + ") ON UPDATE CASCADE"
                + ");");

db.execSQL("CREATE TABLE IF NOT EXISTS " +Table.PAYMENT
+ " (" + PaymentSchema.ID + " INTEGER PRIMARY KEY"
+ "," + PaymentSchema.KIND + " INTEGER NOT NULL"
+ "," + PaymentSchema.NAME + " TEXT NOT NULL UNIQUE"
+ "," + PaymentSchema.TOTAL + " TEXT NOT NULL"
+ "," + PaymentSchema.HIDDEN + " INTEGER NOT NULL"
+ ");");

Any body can help me to solve this problem?

任何人都可以帮我解决这个问题吗?

Actually I want to update the id from 4 to 5 I can ensure both 4 and 5 are the exist IDs of the payment table.

实际上我想将id从4更新为5我可以确保4和5都是付款表的现有ID。

But the same problem occurred, so I update 4 to 4 is the same. I think if I can solve the problem of 4 to 4 , I should solve the problem of 4 to 5 .

但是同样的问题发生了,所以我更新4到4是一样的。我想如果我能解决4到4的问题,我应该解决4到5的问题。

Many thanks for your answer!!

非常感谢你的回答!!

1 个解决方案

#1


0  

I have found the answer. One of my team run the following

我找到了答案。我的一个团队运行以下内容

db.execSQL("PRAGMA foreign_keys = OFF;");

then put the 0 into the ID_OUT_PAYMENT of record table . The 0 is not the exist id of the PAYMENT table.

然后将0放入记录表的ID_OUT_PAYMENT中。 0不是PAYMENT表的存在id。

So when I update the ID_OUT_PAYMENT it will trigger the constraint failed.

因此,当我更新ID_OUT_PAYMENT时,它将触发约束失败。

So If I want to make my sql above run successfully. I need to run the sql too.

所以,如果我想让我的sql上面运行成功。我也需要运行sql。

 db.execSQL("PRAGMA foreign_keys = OFF;");

Thanks very much for every one to reply me!!

非常感谢每一个人回复我!

Thanks!

#1


0  

I have found the answer. One of my team run the following

我找到了答案。我的一个团队运行以下内容

db.execSQL("PRAGMA foreign_keys = OFF;");

then put the 0 into the ID_OUT_PAYMENT of record table . The 0 is not the exist id of the PAYMENT table.

然后将0放入记录表的ID_OUT_PAYMENT中。 0不是PAYMENT表的存在id。

So when I update the ID_OUT_PAYMENT it will trigger the constraint failed.

因此,当我更新ID_OUT_PAYMENT时,它将触发约束失败。

So If I want to make my sql above run successfully. I need to run the sql too.

所以,如果我想让我的sql上面运行成功。我也需要运行sql。

 db.execSQL("PRAGMA foreign_keys = OFF;");

Thanks very much for every one to reply me!!

非常感谢每一个人回复我!

Thanks!