Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
 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? .................................................................................................................................................

Because you are violating the database's constraint. The column UserData.RecieversID may only hold unique values which you seem to ignore in your code while updating. – f1sh Nov 26, 2018 at 21:43

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.

    So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID – user10643013 Nov 27, 2018 at 16:38 You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked. – MikeT Nov 27, 2018 at 19:25

    It looks like you made the same newbie mistake I did. When using db.update to modify row values, you DO NOT specify which row by added KEY_ID to ContentValues. Instead you must use a where clause for this purpose. A where clause is one of the parameters passed to db.update.

    Explanation: ContentValues contains column-name/value pairs to apply to selected row(s). The where clause indicates which row(s) to select. It is possible (and common) to select multiple rows. Passing null for where clause updates every row in the database - so be carefull

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.