0

I got android SQLiteConstraintException: error code 19: constraint failed exception. I saw there are tons of question in this topic, I've read a bunch of them, but still cant figure out what I did wrong. Mere is my table schema.

    private static final String DATABASE_CREATE = "CREATE TABLE calls (call_ID 
    INTEGER        AUTO    INCREMENT PRIMARY KEY NOT NULL, caller_id TEXT NOT NULL, "+
    caller_name TEXT NOT NULL, called_id TEXT NOT NULL, called_name    TEXT NOT NULL," + 
    "start_time TEXT NOT NULL, end_time TEXT NOT NULL);";

I add one single row, and it throws the exception.

   public long addCall(String caller_id, String caller_name, String called_id,
   String called_name, String start, String end) {
      ContentValues initialValues = createContentValues(caller_id, caller_name,called_id,called_name, start,end);
    return database.insert(DATABASE_TABLE, null, initialValues);
}

I overrided the SQLHelpers onOpen method, to clear the table on every open, but it didnt help.

public void onOpen (SQLiteDatabase database){
    database.execSQL("DROP TABLE IF EXISTS calls");
    onCreate(database);
    Log.w(DBHelper.class.getName(),"DB onOpen");
}

UPDATE: The problem is not that, I try to pass null, cause I only add these strings, and I got the error.

    dbAdapter.addCall("test", "test","test", "test", "test", "test");

UPDATE 2: This is my createContentvalue method:

public static final String CALLER_ID = "caller_id";
public static final String CALLER_NAME = "caller_name";
public static final String CALLED_ID = "called_id";
public static final String CALLED_NAME = "called_name";
public static final String START = "start_time";
public static final String END = "end_time";
public static final String DATABASE_TABLE = "calls";

    private ContentValues createContentValues(String caller_id, String caller_name, String called_id, String called_name, String start, String end) {
    ContentValues values = new ContentValues();
    values.put(CALLER_ID, caller_id);
    values.put(CALLER_NAME, caller_name);
    values.put(CALLED_ID, called_id);
    values.put(CALLED_NAME, called_name);
    values.put(START, start);
    values.put(END, end);
    return values;
}
3
  • Is there more info in the exception itself? Maybe which constraint failed? Commented Sep 29, 2011 at 18:54
  • Please show us your createContentValues method. Commented Sep 29, 2011 at 19:04
  • I updated again, ther is my createContentValues method, and this is what the exception says: "09-29 19:12:13.329: ERROR/SQLiteDatabase(904): Error inserting start_time=test caller_id=test end_time=test caller_name=test" Commented Sep 29, 2011 at 19:13

1 Answer 1

1

Checked for null values in your method arguments? ;-)

Edit based on update

Haa, I knew it: A null value. ;-)
You use CALLER_NAME twice... (btw CALLER_ID too)

Edit based on comments

Besides the null value, he was trying to set the primary key (accidentally). That could have been the main reason (besides the actual null value).

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

5 Comments

yes, thats not the problem. I updated my post, I try to insert a dummy raw, and still get the error.
hmm, thats very emberassing, i corrected it, but it did not solved my problem. I still got the same error (thank you for noticing anyway)
That's so simple it must be null. Please check whether you set all needed table column fields and whether your constants contain the right values and not the same column twice. They have such similar names only one letter difference...
I will check, for typos. But I tried to remove the NOT NULL from the schema, so it should accept null values. but still get the same error
Are you trying to set the primary key yourself? Then don't. Check your constants for primary key column. This is my last suggestion. I can't think of anything else...

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.