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

发布于 2024-11-27 04:21:36 字数 2265 浏览 1 评论 0原文

我执行了以下sql:

更新记录集out_ payment = 4 where out_ payment = 4;

记录的out_ payment 被定义为Integer 并引用Payment(id)

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

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

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

代码如下:

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

架构如下:

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"
+ ");");

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

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

但是又出现了同样的问题,所以我更新4到4也是一样。 我想如果我能解决4到4的问题,我就应该解决4到5的问题。

非常感谢您的回答!!

I have execute the following sql:

update record set out_payment=4 where out_payment=4;

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

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

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.

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 .

Many thanks for your answer!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-12-04 04:21:36

我已经找到答案了。
我的团队之一运行以下命令

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

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

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

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

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

非常感谢大家回复我!!

谢谢!

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.

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

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

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

Thanks very much for every one to reply me!!

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文