Skip to content
Advertisement

android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed Couldnot figure out what the error says

I have got stuck in the above-mentioned error message in Android Studio. My Database is a simple one 1st column: Name TEXT PRIMARY KEY 2nd column: Price TEXT

I have already gone through the answers to the same question in StackOverflow but couldn’t resolve the error.

I am quoting my DataBaseHelper class and insertActivity here:

JavaScript
JavaScript

error:

JavaScript

Advertisement

Answer

These lines:

JavaScript

assign to the variables Name and Price the text of the EditTexts itemName and itemPrice when the insertActivity loads (maybe they are just empty strings) and they are never changed.
So when you click the button the listener by calling dataBaseHelper.addItem(Name,Price) tries to insert the same values in the table and this results to the error you get because since the column Name is the Primary Key of the table it must be unique.

What you must do is move these lines inside the listener:

JavaScript

so when you click the button the correct values are retrieved from the EditTexts and then inserted in the table.

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