4

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

error in my application and not sure why.

It happens when i add a new Inbox item into my database.

here is the Create statement of my inbox table:

CREATE TABLE inbox (uid text, title text, sender text, timeRecieved text, PRIMARY KEY(uid));

implemented using these static final Strings;

public static final String LABEL_INBOX_UID = "uid";
    public static final String LABEL_INBOX_TITLE = "title";
    public static final String LABEL_INBOX_SENDER = "sender";
    public static final String LABEL_INBOX_TIME_RECIEVED = "timeRecieved";

    public static final int COLUMN_INBOX_UID = 0;
    public static final int COLUMN_INBOX_TITLE  = 1;
    public static final int COLUMN_INBOX_SENDER= 2;
    public static final int COLUMN_INBOX_TIME_RECIEVED = 3;

    public static final String TABLE_NAME_INBOX = "inbox";
    public static final String TABLE_CREATE_INBOX = "CREATE TABLE "
        + TABLE_NAME_INBOX + " (" + LABEL_INBOX_UID + " text, "
        + LABEL_INBOX_TITLE + " text, " + LABEL_INBOX_SENDER
        + " text, " + LABEL_INBOX_TIME_RECIEVED + " text, "
     + "PRIMARY KEY(" + LABEL_INBOX_UID + ")" + ");";

And here is the method that gets a inbox item and put it inside a contentValues()

@Override
    protected void insertItem(InboxListItem object, String table) {
        InboxListItem item = object;
        inboxItemValue = new ContentValues();

        inboxItemValue.put(LABEL_INBOX_UID, item.getUid()); 
        Log.d(TAG, "item.getUid() " + item.getUid());
        inboxItemValue.put(LABEL_INBOX_TIME_RECIEVED, item.getTimeRecieved());
        Log.d(TAG, "item.getTimeRecieved() " + item.getTimeRecieved());
        inboxItemValue.put(LABEL_INBOX_TITLE, item.getTitle());
        Log.d(TAG, "item.getTitle() " + item.getTitle());
        inboxItemValue.put(LABEL_INBOX_SENDER, item.getSender());
        Log.d(TAG, "item.getSender() " + item.getSender());


        Log.d(TAG, "TABLE_CREATE_EVENTS =" + TABLE_CREATE_EVENTS);
        Log.d(TAG, "TABLE_CREATE_INBOX =" + TABLE_CREATE_INBOX);
        Log.d(TAG, "TABLE_CREATE_CONTACTS =" + TABLE_CREATE_CONTACTS);
        database().insert(TABLE_NAME_INBOX, null, inboxItemValue);
        Log.d(TAG, "database().insert(TABLE_NAME_INBOX, null, inboxItemValue);");


    }

O and here is the full exception message:

11-22 12:24:39.606: ERROR/Database(773): Error inserting sender=Mit Technology Review uid=663b074a-16c8-46fb-a420-e0ebf716e212 title=Emerging Technologies Friday Update (11/19/2010) timeRecieved=2010-11-19T06:02:36.000Z 11-22 12:24:39.606: ERROR/Database(773): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 11-22 12:24:39.606: ERROR/Database(773):
at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 11-22 12:24:39.606: ERROR/Database(773): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 11-22 12:24:39.606: ERROR/Database(773): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 11-22 12:24:39.606: ERROR/Database(773): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.InboxDbWrapper.insertItem(InboxDbWrapper.java:44) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.InboxDbWrapper.insertItem(InboxDbWrapper.java:1) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.DbWrapper.insertAllItemsToDb(DbWrapper.java:42) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.InboxDbWrapper.populateTable(InboxDbWrapper.java:54) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.main.screen.HomeMainScreenOne.loadDataToDb(HomeMainScreenOne.java:225) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.main.screen.AbstractMainScreen.onCreate(AbstractMainScreen.java:125) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.main.screen.HomeMainScreenOne.onCreate(HomeMainScreenOne.java:51) 11-22 12:24:39.606: ERROR/Database(773): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-22 12:24:39.606: ERROR/Database(773): at android.os.Handler.dispatchMessage(Handler.java:99) 11-22 12:24:39.606: ERROR/Database(773): at android.os.Looper.loop(Looper.java:123) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-22 12:24:39.606: ERROR/Database(773): at java.lang.reflect.Method.invokeNative(Native Method) 11-22 12:24:39.606: ERROR/Database(773): at java.lang.reflect.Method.invoke(Method.java:521) 11-22 12:24:39.606: ERROR/Database(773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-22 12:24:39.606: ERROR/Database(773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-22 12:24:39.606: ERROR/Database(773): at dalvik.system.NativeStart.main(Native Method)

Thanks in advance:

Edit: Somemore info. I had previously created a events and contactsList table succesfully and that seems to work fine. here is the code below:

Contacts:

// Variables used to create the events table
    public static final String TABLE_NAME_CONTACTS = "contactsList";

    public static final String LABEL_CONTACTS_UID = "uid";
    public static final String LABEL_CONTACTS_FULLNAME = "fullname";

    public static final String TABLE_CREATE_CONTACTS = "CREATE TABLE "
            + TABLE_NAME_CONTACTS + " (" + LABEL_CONTACTS_UID + " text, "
            + LABEL_CONTACTS_FULLNAME + " text, " + "PRIMARY KEY("
            + LABEL_CONTACTS_UID + ")" + ");";


    public static final int COLUMN_CONTACTS_UID = 0;
public static final int COLUMN_CONTACTS_FULLNAME = 1;

CREATE TABLE contactsList (uid text, fullname text, PRIMARY KEY(uid));

calendar events:

public static final String LABEL_EVENTS_UID = "uid";
    public static final String LABEL_EVENTS_TITLE = "title";
    public static final String LABEL_EVENTS_LOCATION = "location";
    public static final String LABEL_EVENTS_DATE = "date";
    public static final String LABEL_EVENTS_TIME = "time";
    public static final String LABEL_EVENTS_TIMEZONE = "timezone";

    public static final int COLUMN_EVENTS_UID = 0;
    public static final int COLUMN_EVENTS_TITLE = 1;
    public static final int COLUMN_EVENTS_LOCATION = 2;
    public static final int COLUMN_EVENTS_DATE = 3;
    public static final int COLUMN_EVENTS_TIME = 4;
    public static final int COLUMN_EVENTS_TIMEZONE = 5;

    // Variables used to create the events table
    public static final String TABLE_NAME_EVENTS = "events";
    public static final String TABLE_CREATE_EVENTS = "CREATE TABLE "
            + TABLE_NAME_EVENTS + " (" + LABEL_EVENTS_UID + " text, "
            + LABEL_EVENTS_TITLE + " varchar(30), " + LABEL_EVENTS_LOCATION
            + " varchar(30), " + LABEL_EVENTS_DATE + " varchar(30), "
            + LABEL_EVENTS_TIME + " varchar(30), " + LABEL_EVENTS_TIMEZONE
            + " varchar(30), " + "PRIMARY KEY(" + LABEL_EVENTS_UID + ")" + ");";

CREATE TABLE events (uid text, title varchar(30), location varchar(30), date varchar(30), time varchar(30), timezone varchar(30), PRIMARY KEY(uid));

4 Answers 4

9

The only constraint on your table is the primary key, so you're probably trying to insert an item with a non-unique 'UID'.

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

3 Comments

umm possibly but im getting the uid's from a database table hosted from a server. see my edited OP as i have created an event and contactList table and both work fine
I'm 99% sure that this is your problem. Maybe you're getting duplicates by mistake, or the server database doesn't have this constraint. You could just check if you have the uid in your database before attempting an insert.
cheers you was right. my code is fine, its the web service im using thats not. returning me duplicated
3

your primary key is not autoincrement, so you should be responsible to generate unique one each time you insert a record in the table. You can change this behaviour by this:

CREATE TABLE inbox (uid integer PRIMARY KEY AUTOINCREMENT, title text, sender text, timeRecieved text);

1 Comment

oh, sorry, misattention. Of course, you can't. Are you sure you need to have text instead of integer?
1

@jonney As you said you have added a new table content. or if you update your db version the same error will occur, But no need to worry, You done it Buddy the valid reason is primary key is not autoincrement But solution is Easy Just Uninstall the app and Install it again, One of my Sr. Fraind told me this and it works. Hope Your Problem will be Solved. Best Luck.

2 Comments

its sorted. i answered this quetsion nearly two years ago lol
Sorry Buddy, But I have not seen...Nice your work done 2 year ago, That's good.
0

Try this:

return db.insertWithOnConflict(YOURTABLENAME, null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);

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.