1
I am using Sqlite for a local bank in an Android application, my problem is that I am unable to handle the integrity 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 treat this exception to give a message to the user, I have already used both Exception, as well as Sqliteexception, Sqliteconstraintexception. Follows 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;
}
Thanks, it worked out!
– Bruno Inácio