Sqliteconstraintexception: Column Id Is Not Unique (code 19) Exception When Updating Row
I have a table in my database, when adding new record I am checking if the record exists using the id.If it does, I want to update the record and if it doesn't add a new record. Bu
Solution 1:
Replace your query with the below
StringQuery="Select * from " + tableName + " where " + DEBTOR_ID + " = '" + value+"'";
since selection value
is string , it must be enclosed in single quote.
Updated : Instead of writing whole bunch code for checking whether record exits or not if exits then udpating that record, you can replace all these things with a simple single line.
database.insertWithOnConflict(table,null,values,SQLiteDatabase.CONFLICT_REPLACE);
It will internally handles the exception, CONFLICT_REPLACE
it specifies that When a UNIQUE constraint violation occurs, the pre-existing rows that are causing the constraint violation are removed prior to inserting or updating the current row.
Post a Comment for "Sqliteconstraintexception: Column Id Is Not Unique (code 19) Exception When Updating Row"