15

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?

2
  • When you expect others to debug your code, its best if you provide the entire stacktrace. Commented Nov 14, 2011 at 5:44
  • make sure,is your table properly created?i think,your create query is having problem too. you didn't give a white space after table name in your create query.also column names you are using to retrieve data from table are different than you are using those in create query. Commented Nov 14, 2011 at 5:56

2 Answers 2

33

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you post your insert method as well? I assume it's a helper method that calls the actual SQLiteDatabase.insert method with your data
perfect solution for table without primary keys!!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.