using the SQLite on Android I have a program where I ask for the name, phone number and address, and I also have a "Update" button to update some data, my question is:
How do I create a sentence where I press the "Update" button and it looks first if the record exists, and if it exists to update it. In case you do not find the register that registers it.
I tried to put the content of the "Update" method into a try {} catch {} and include the method to register within the catch but when I run the application it never enters the catch.
Here I leave my code of how to update a user that does exist, and also how to register a user, how could you join both codes to get the result you want? Thank you very much
private void actualizarUsuario() {
SQLiteDatabase db=conn.getWritableDatabase();
String[] parametros={campoId.getText().toString()};
ContentValues values=new ContentValues();
values.put(Utilidades.CAMPO_NOMBRE,campoNombre.getText().toString());
values.put(Utilidades.CAMPO_TELEFONO,campoTelefono.getText().toString());
db.update(Utilidades.TABLA_USUARIO,values,Utilidades.CAMPO_ID+"=?",parametros);
Toast.makeText(getApplicationContext(),"Ya se actualizó el usuario",Toast.LENGTH_LONG).show();
db.close();
}
//METODO PARA REGISTRAR USUARIO
private void registrarUsuarios() {
ConexionSQLiteHelper conn=new ConexionSQLiteHelper(this,"bd_usuarios",null,1);
SQLiteDatabase db=conn.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Utilidades.CAMPO_ID,campoId.getText().toString());
values.put(Utilidades.CAMPO_NOMBRE,campoNombre.getText().toString());
values.put(Utilidades.CAMPO_TELEFONO,campoTelefono.getText().toString());
Long idResultante=db.insert(Utilidades.TABLA_USUARIO,Utilidades.CAMPO_ID,values);
Toast.makeText(getApplicationContext(),"Id Registro: "+idResultante,Toast.LENGTH_SHORT).show();
db.close();
}