Android sqlite 约束失败错误代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3371307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sqlite constraint failed error code
提问by Mojo Risin
I have two tables albums and songs and each song has reference to alum's id.
我有两张桌子专辑和歌曲,每首歌都引用了 alum 的 id。
Album table :
专辑表:
CREATE TABLE albums
(id INTEGER PRIMARY KEY ASC,
name TEXT,
additional TEXT)
Song table:
歌曲表:
CREATE TABLE songs
(id INTEGER PRIMARY KEY ASC,
album_fk INTEGER NOT NULL,
title TEXT,
url TEXT,
duration BIGINT NOT NULL)
and i also have a trigger to check when i delete an album if there are existing songs in it :
我还有一个触发器来检查我何时删除专辑中是否有现有歌曲:
CREATE TRIGGER trigger_on_delete
BEFORE DELETE ON albums
FOR EACH ROW BEGIN
SELECT RAISE(FAIL,'album has songs cannot be deleted')
WHERE (SELECT album_fk FROM songs WHERE album_fk = OLD.id) IS NOT
NULL;)
END
all this is working fine but sometimes
所有这些工作正常,但有时
Exception: android.database.sqlite.SQLiteConstraintException: error
code 19: constraint failed
Stack Trace :
android.database.sqlite.SQLiteStatement.native_execute(Native Method)
android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:
66)
is thrown when i try to delete an album. My delete method is simply deleting an album by id.The bad thing is that i can't reproduce this so there is no much info i can provide. I tried different scenarios with deleting an album but i never got this exception.
当我尝试删除专辑时抛出。我的删除方法只是通过 id 删除专辑。不好的是我无法重现此内容,因此我无法提供太多信息。我尝试了不同的删除专辑的场景,但我从来没有遇到过这个例外。
So any ideas how can investigate this issue. There are not any contraints in album table so what could cause such exception ? Any help will be appreciated.
所以任何想法如何调查这个问题。专辑表中没有任何限制,那么什么会导致这种异常?任何帮助将不胜感激。
采纳答案by Mojo Risin
This is so not fair. If you run this code on PC everything is OK and if you try to delete album that has songs the exception says :album has songs cannot be deleted" but it seems that android guys don't handle correct RAISE from sqlite and jsut throw Exception: android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed.
这太不公平了。如果您在 PC 上运行此代码,一切正常,并且如果您尝试删除包含歌曲的专辑,则异常显示:专辑中的歌曲无法删除”,但似乎 android 人员无法处理来自 sqlite 的正确 RAISE 和 jsut 抛出异常: android.database.sqlite.SQLiteConstraintException:错误代码 19:约束失败。
回答by julian velazquez
I solved this by correcting the definition of my database to a field that is NOT NULL. You must send some data.
我通过将数据库的定义更正为非空字段来解决此问题。您必须发送一些数据。
回答by Youyougoslave
I had the same problem with some triggers in the backgroung, I finally switched to this classic option and it fixed my problem:
我在后台的一些触发器上遇到了同样的问题,我终于切换到了这个经典选项,它解决了我的问题:
db.rawQuery("UPDATE table set column = newValue WHERE ... ;", null);
db.rawQuery("UPDATE table set column = newValue WHERE ... ;", null);
回答by MikeW
See also "SQLiteStatement does not propagate error messages defined in SQLite triggers" http://code.google.com/p/android/issues/detail?id=3296
另请参阅“SQLiteStatement 不会传播 SQLite 触发器中定义的错误消息” http://code.google.com/p/android/issues/detail?id=3296