android.database.sqlite.SQLiteConstraintException:错误代码19:约束失败异常

发布于 2024-12-15 02:11:50 字数 1930 浏览 4 评论 0原文

我创建了一个名为resources的表,但是当我在其中插入值时,会抛出此异常:

android.database.sqlite.SQLiteConstraintException:
error code 19: constraint failedexception

这是我的创建表语句:

public static final String DATABASE_CREATE = 
    "CREATE TABLE " + table_resources + "(ID INTEGER PRIMARY KEY, 
    KEY_TYPE text, KEY_ENCODING text, KEY_WIDTH text, KEY_HEIGHT text, 
    KEY_DATA text, KeyIId text)";

以下是我的插入 em> code:

JSONObject show = data.getJSONObject(i);
if (show.get("type").equals("resource_updates")) {
    JSONArray resources = show.getJSONArray("resources");
    try {
        System.out.println("length of resources is is " + resources.length());
        for (int resourceIndex = 0; resourceIndex < resources.length(); resourceIndex++) {
            type = resources.getJSONObject(resourceIndex).getString("type").toString();
            encoding = resources.getJSONObject(resourceIndex).getString("encoding").toString();
            data1 = resources.getJSONObject(resourceIndex).getString("data").toString();
            id = resources.getJSONObject(resourceIndex).getString("id").toString();
            try {
                width = resources.getJSONObject(resourceIndex).getString("width").toString();
            } catch (Exception e) {
                System.out.println(e);
                width = "null";
            }
            try {
                height = resources.getJSONObject(resourceIndex).getString("height").toString();
            } catch (Exception e) {
                e.printStackTrace();
                height = "null";
            }
            db.insert(type,encoding,width,height, data1,iid);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e + "exception");
        System.out.println("exception in  the resources");
    }
}

谁能告诉我问题出在哪里?

I created a table named resources but when I insert values in it, this exception is thrown:

android.database.sqlite.SQLiteConstraintException:
error code 19: constraint failedexception

Here is my create table statement:

public static final String DATABASE_CREATE = 
    "CREATE TABLE " + table_resources + "(ID INTEGER PRIMARY KEY, 
    KEY_TYPE text, KEY_ENCODING text, KEY_WIDTH text, KEY_HEIGHT text, 
    KEY_DATA text, KeyIId text)";

The following is my insert code:

JSONObject show = data.getJSONObject(i);
if (show.get("type").equals("resource_updates")) {
    JSONArray resources = show.getJSONArray("resources");
    try {
        System.out.println("length of resources is is " + resources.length());
        for (int resourceIndex = 0; resourceIndex < resources.length(); resourceIndex++) {
            type = resources.getJSONObject(resourceIndex).getString("type").toString();
            encoding = resources.getJSONObject(resourceIndex).getString("encoding").toString();
            data1 = resources.getJSONObject(resourceIndex).getString("data").toString();
            id = resources.getJSONObject(resourceIndex).getString("id").toString();
            try {
                width = resources.getJSONObject(resourceIndex).getString("width").toString();
            } catch (Exception e) {
                System.out.println(e);
                width = "null";
            }
            try {
                height = resources.getJSONObject(resourceIndex).getString("height").toString();
            } catch (Exception e) {
                e.printStackTrace();
                height = "null";
            }
            db.insert(type,encoding,width,height, data1,iid);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e + "exception");
        System.out.println("exception in  the resources");
    }
}

Can anyone tell me where could be the problem?

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

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

发布评论

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

评论(2

云归处 2024-12-22 02:11:50

约束失败通常表示您执行了诸如将 null 值传递到创建表时声明为 not null 的列之类的操作。

Constraint failed usually indicates that you did something like pass a null value into a column that you declare as not null when you create your table.

茶底世界 2024-12-22 02:11:50

为了获得有关 SQLite 错误的更多信息,可以使用 ADB dumpsys:

adb shell dumpsys dbinfo -v

结合 grep:

adb shell dumpsys dbinfo -v | grep executeForLastInsertedRowId

或 grep'ed 两次:

adb shell dumpsys dbinfo -v | grep executeForChangedRowCount | grep failed

In order to get more information about SQLite errors, one can use ADB dumpsys:

adb shell dumpsys dbinfo -v

combined with grep:

adb shell dumpsys dbinfo -v | grep executeForLastInsertedRowId

or grep'ed twice:

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