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

我创build了一个名为资源的表,但是当我插入值的时候,抛出了这个exception:

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

这是我的创build表声明:

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

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

Solutions Collecting From Web of "android.database.sqlite.SQLiteConstraintException:错误代码19:约束failedexception"

约束失败通常表示您做了类似于将null值传递到您在创build表时声明not null的列。

为了获得更多关于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