15

我创建了一个名为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)";

以下是我的插入代码:

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

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

4

2 回答 2

33

约束失败通常表示您执行了一些操作,例如将值传递到您在创建表时null声明的列。not null

于 2011-11-14T05:33:03.087 回答
0

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

adb shell dumpsys dbinfo -v

结合grep:

adb shell dumpsys dbinfo -v | grep executeForLastInsertedRowId

或两次grep:

adb shell dumpsys dbinfo -v | grep executeForChangedRowCount | grep failed
于 2016-07-17T01:58:17.180 回答