I am using SQLite for a local bank in an Android application, my problem is that I am not able to handle health restriction exceptions. I have a table where the two fields are PK, that is, there can never be equal records. When I try to enter two equal data it throws the exception:
E/SQLiteDatabase: Error inserting DIA=08-03-2018 HORA=02:06
android.database.sqlite.SQLiteConstraintException:
UNIQUE constraint failed: TBPONTOSTEMP.DIA, TBPONTOSTEMP.HORA (code 1555)
I need to handle this exception to give a message to the user, I have already used both the Exception and the SQLiteException, SQLiteConstraintException. Here is the code:
public int cadastrarPontosTemp(String ponto, String dia){
ContentValues values = new ContentValues();
try{
values.put("DIA",dia);
values.put("HORA",ponto);
getWritableDatabase().insert("TBPONTOSTEMP",null,values);
Log.e("cadastrarPontosTemp","SUCESSO");
}catch (SQLiteConstraintException e){
Log.e("cadastrarPontosTemp",e.getMessage());
return 1;
}
return 0;
}