Skip to content
Advertisement

android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed:

Error

 FATAL EXCEPTION: main
                                                                             Process: com.appmaster.akash.messageplus, PID: 14373
                                                                             android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: UserData.RecieversID (code 1555)
                                                                                 at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
                                                                                 at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
                                                                                 at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
                                                                                 at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
                                                                                 at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1579)
                                                                                 at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
                                                                                 at com.appmaster.akash.messageplus.Chat.mMessagesSent(Chat.java:918)
                                                                                 at com.appmaster.akash.messageplus.Chat.sendMessage(Chat.java:709)
                                                                                 at com.appmaster.akash.messageplus.Chat.access$900(Chat.java:76)
                                                                                 at com.appmaster.akash.messageplus.Chat$5.onClick(Chat.java:442)
                                                                                 at android.view.View.performClick(View.java:6256)
                                                                                 at android.view.View$PerformClick.run(View.java:24701)
                                                                                 at android.os.Handler.handleCallback(Handler.java:789)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                 at android.os.Looper.loop(Looper.java:164)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Inserting

 MainData helper = new MainData(this); //Change the name to your Helper Class name
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID, MessageRecieverId);
    contentValues.put(KEY_NAME, MessageRecieverName);
    contentValues.put(KEY_MESSAGES_SENT, 0);
    contentValues.put(KEY_MESSAGES_RECIEVED, 0);
    contentValues.put(KEY_TIME_SPENT, "");

    long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
    if (returnVariable == -1) {
        Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();

No problem in inserting but while updating im getting this error

Updating

 MainData helper = new MainData(this); //Change the name to your Helper Class name
    SQLiteDatabase db = helper.getWritableDatabase();int userData = 0;
    Cursor data2 = helper.getUserData();
    while (data2.moveToNext()) {
        userData = data2.getInt(data2.getColumnIndex("MessagesSent"));
    }
    ContentValues contentValues2 = new ContentValues();
    contentValues2.put(KEY_ID, MessageRecieverId);
    contentValues2.put(KEY_MESSAGES_SENT, userData+1);
    long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,null,null);
    if (returnVariable2 == -1) {
        Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
        //-1 means there was an error updating the values
    } else {
        Toast.makeText(getApplication(),"uf", Toast.LENGTH_SHORT).show();
    }

Anyone know why this is so? ……………………………………………………………………………………………………………………………….

Advertisement

Answer

You are likely inserting using a value for the id column (KEY_ID). Odds on this column is defined as either INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT.

Typically you do not specify a value for such a column as SQLite will assign a unique value (typically 1 greater than the highest assigned).

If you specify a value for such a column and a row in the table already has that value in the column then the UNIQUE constraint implied by PRIMARY KEY will be violated as it’s against the rule that a PRIMARY KEY has to be unique.

I’d suggest that you want to use :-

MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(KEY_ID, MessageRecieverId); //<<<<<<<<<< COMMENTED OUT (can delete the line)
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");

long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
    Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();

returnVariable will then be the value of the id column (KEY_ID) that was assigned when the row was inserted.

  • Note id column does not necessarily mean that the column is named id but that it’s a special column due to how it is defined. That is, by specifying INTEGER PRIMARY KEY, the column is actually an alias of the normally hidden rowid column (unless the TABLE is defined with the WITHOUT ROWID phrase).

You may find reading SQLite Autoincrement helpful.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement