Skip to content
Advertisement

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

Error

Inserting

No problem in inserting but while updating im getting this error

Updating

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 :-

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