ORMLite handle uniqueCombo failure exception

I have a couple of columns in my database that have 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 must be unique in the table. To test this, I intentionally add the same fields. I am getting SQLException but not sure how I should 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 done.

- UPDATE ---

SQLException getErrorCode and getSQLState 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

      

+3


source to share


2 answers


I am getting SQLException but not sure how I should handle this exception and ask the user to make changes.

You can of course catch the exception and see if there is one instanceof SQLiteConstraintException

, but I always hate to handle exceptions as normal occurrences.



What I would do is ask for user input to see if an existing object exists MyInfo

with the same fields. Something like the following:

QueryBuilder<MyInfo, Integer> qb = mydao.queryBuilder();
qb.where().eq("COL1", myInfo.col1).and().eq("COL2", myInfo.col2);
if (qb.queryForFirst() != null) {
   // tell the user to enter unique values
}

      

+1


source


This is not the best way to do it, but it might help someone ...

try {
        //insert to ORMLite
} catch (SQLException e) {
        validateException(e);
}

      



in the validateException method:

private void validateException(SQLException e) {
        try{
            if (e.getCause() == null)
                throw e;
            if (e.getCause().getCause() == null)
                throw e;
            if (e.getCause().getCause() instanceof SQLiteConstraintException)
                Log.d("Test", "Ignoring duplicate");
            else
                throw e;
        }catch(SQLException e1){
            //exception thrown by e;
        }
    }

      

0


source







All Articles