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

时间:2011-11-14 05:29:27

标签: android database sql-insert

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

谁能告诉我哪里可能出现问题?

2 个答案:

答案 0 :(得分:32)

约束失败通常表示您在创建表时将null值传递给您声明为not null的列。

答案 1 :(得分:0)

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

adb shell dumpsys dbinfo -v

结合grep:

adb shell dumpsys dbinfo -v | grep executeForLastInsertedRowId

或grep&#39;两次:

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