Skip to content
Advertisement

UNIQUE constraint failed: sqlite database : android

I am trying to insert values in table. But there is only one value inserted. I am getting an error in log cat when I am trying to insert new values.

Log cat shows :

JavaScript

Its showing error on these two lines while inserting row.

JavaScript

EventTableHelper

JavaScript

How to solve this??

Advertisement

Answer

Your code probably violates primary key’s uniqueness constraint on a KEY_ID field.

Two possible solutions are:

  1. Make sure that your EventData.getId() returns unique values per object. For now, I don’t see you pass any identifier to its constructor and perhaps all the events are inserted with the same id value.
  2. If you don’t care about generating ids by yourself, you can add AUTOINCREMENT setting to your KEY_ID column definition. This way KEY_ID field will be filled automatically and each row will have its own, unique value. Once there, don’t forget to remove adding KEY_ID to ContentValues by yourself.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement