I have a couple of columns in my database which has been annotated as uniqueCombo=true
@DatabaseField(columnName = "COL1", uniqueCombo = true)
private Double col1;
@DatabaseField(columnName = "COL2", uniqueCombo = true)
private Double col2;
According to ormlite documentation the combination of these two fields should be unique in the table. To test this I am purposely adding same fields. I do get the SQLException but am not sure how do I handle this exception and ask the user to make changes.
try {
mydao.create(myInfo);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
/* Need to uniquely identify constraint failed error here
* and ask user to make suitable change */
}
Any idea how this can be accomplished.
— UPDATE —
SQLException getErrorCode and getSQLState both return 0 and null respectively.
Logcat StackTrace:
Caused by: android.database.sqlite.SQLiteConstraintException: column NAME is not unique (code 19)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
01-31 22:15:14.042: W/System.err(2586): at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:122)
01-31 22:15:14.042: W/System.err(2586): ... 15 more
You can certainly catch the exception and see if it is
instanceof SQLiteConstraintExceptionbut I always hate treating exceptions as common occurrences.What I would do is do a query on the user’s input to see if there is an existing
MyInfoobject with the same fields. Something like the following: