Search code examples
validationandroid-sqliteconstraints

How to recognize what constraint cause an exception in an insertion in Sqlite


How could I know what type of constraint exception is raised in an insertion in Sqlite. What it caused?. For example if I design a table with an unique constraint and primary key constraint, and I insert a row that violate a constraint, sqlite will raise an SqliteConstraintException, but I would like to know which of both constraints was violated. That would be helpful for creating an error message for the user. Instead of writing functions that validate the row before inserting it. Thank you for your help.


Solution

  • That information is not provided as a field on the Exception. You can use the getMessage() method to get some information about the cause. Like UNIQUE constraint failed: event.id as an example. This is telling you the name and the column which violated the constraint, but not the name of the constraint which is violated.