获取造成SQLiteConstraintException约束的名字 [英] Get name of constraint which caused SQLiteConstraintException
问题描述
我如何得到这引起了SQLiteConstraintException约束的名称。
How do I get the name of the constraint which caused the SQLiteConstraintException.
调用toString()的异常只是给了我:错误code 19:约束失败
Calling toString() on the exception just gives me: "error code 19: constraint failed"
和存在的例外都没法得到的原因。这使得很难调试我的sql。
And there is no method in the exception to get the cause. This makes it rather difficult to debug my sql.
推荐答案
版本 3.7.17开始,SQLite的显示错误消息约束的名称:
Beginning with version 3.7.17, SQLite shows the name of the constraint in error messages:
sqlite> create table t(x, constraint x_positive check (x>0));
sqlite> insert into t values(-1);
Error: constraint x_positive failed
不过,这将是一段时间,在此之前的版本出现在Android的。
However, it will be some time before this version shows up in Android.
在此期间,你可以更换一个触发,可以使用约束任何消息时,它喜欢的:
In the meantime, you could replace the constraint with a trigger that can use whatever message it likes:
CREATE TRIGGER check_x_positive
BEFORE INSERT ON t -- also UPDATE
FOR EACH ROW
WHEN NEW.x <= 0
BEGIN
SELECT RAISE(ABORT, 'RED ALERT! database on fire because x <= 0');
END;
这篇关于获取造成SQLiteConstraintException约束的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!