android.database.sqlite.SQLiteConstraintException:错误代码19:约束failedexception

时间:2021-07-05 05:28:42

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

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

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

Here is my create table statement:

这是我的create table语句:

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?

谁能告诉我哪里可能是问题?

2 个解决方案

#1


32  

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.

约束失败通常表示您执行的操作类似于将空值传递到您在创建表时声明为非null的列。

#2


0  

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

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

adb shell dumpsys dbinfo -v

combined with grep:

结合grep:

adb shell dumpsys dbinfo -v | grep executeForLastInsertedRowId

or grep'ed twice:

或两次grep'ed:

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

#1


32  

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.

约束失败通常表示您执行的操作类似于将空值传递到您在创建表时声明为非null的列。

#2


0  

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

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

adb shell dumpsys dbinfo -v

combined with grep:

结合grep:

adb shell dumpsys dbinfo -v | grep executeForLastInsertedRowId

or grep'ed twice:

或两次grep'ed:

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