How to know if a record already exists before updating it using SQLite on Android?

1

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();
}
    
asked by Pedro Narrea 16.12.2018 в 05:58
source

1 answer

0

I have no idea what the names of your fields in your database would be, but you could use a Cursor to consult your registry and with a if you would do the update process. For example:

    Cursor cursor = db.rawQuery("SELECT * FROM tuTabla WHERE nombre=LeMandasElNombre and id!=ElIdDelRegistroParaActualizar", null);

    if (cursor.moveToNext()) { Si el cursor tiene un registro, quiere decir que ya existe.
         Toast.makeMessage(this, "Ya existe un registro con ese nombre", Toast.LENGTH_LONG).show();
    } else { Sino tiene un registro, entonces actualizamos 
        Aquí metes el código para actualizar el registro
    } 
cursor.close(); //Cerramos nuestro cursor
db.close(); //Cerramos nuestro el db

The logic of the query is that you will do select to search for the record, the parameters of WHERE are the nombre and the Id of the record you want to update, the name is equal to the name that ends to enter as new, and the Id would be different from the record you want to update, in this way the select will search all those records that have the data Nombre but that are different from the Id you want to update.

    
answered by 16.12.2018 / 06:28
source