jeudi 24 mars 2016

How to get error code of SQLiteConstraintException in Java

I'm trying to catch the following exception specifically for code 19. Is there any way of getting the error code of SQLiteConstraintException ? or should I simply have a "contains" check for the message description ?

03-24 14:46:45.430: W/System.err(2219): android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)

        try {
            res = database.insertOrThrow(EMP_TABLE, null, values);      
        } catch (android.database.sqlite.SQLiteConstraintException e) {
             e.printStackTrace();
             //something like e.getErrorCode(); }

My workaround solution is ;

    if(e.getMessage().contains("code 19"))
      Log.d(TAG, "Key Already exists !");

Aucun commentaire:

Enregistrer un commentaire