Error deleting record in SqlLite

1

I am new to programming and I have many doubts.

The first thing is that I'm doing a registry of people, I have the DB in sqlite, I have already managed to save, search and delete the information, this I show in a listview , but when deleting several data the application stops, for example:

I have 3 records, I delete the first one and everything goes well, but I try to select the second one and it does not work anymore and the third one takes it as if it were the second one. I would really appreciate it if you could help me:)

 private  void consultar(){
    DBHelper helper = new DBHelper(this,"Persona",null,1);
    SQLiteDatabase db = helper.getReadableDatabase();
    String sql = "select ID, nombre, direccion, telefono from Persona";
    Cursor c = db.rawQuery(sql,null);
    ArrayList<String> listado = new ArrayList<String>();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item, listado);
    lista.setAdapter(adapter);
    if(c.moveToFirst()){
        do{
            String linea = c.getInt(0)+" "+c.getString(1)+" "+c.getString(2)+" "+c.getString(3);
            listado.add(linea);

        }while(c.moveToNext());
    }
    db.close();
}
private void btnGuardar(String nombre, String direccion, String telefono){
    DBHelper helper = new DBHelper(this,"Persona",null,1);
    SQLiteDatabase db = helper.getWritableDatabase();
    try
    {
        ContentValues c = new ContentValues();
        c.put("nombre",nombre);
        c.put("direccion",direccion);
        c.put("telefono",telefono);
        db.insert("Persona",null,c);
        db.close();
        Toast.makeText(this,"Registro insertado...",Toast.LENGTH_SHORT).show();
    }

    catch (Exception e){
        Toast.makeText(this,"Error: "+e.getMessage(),Toast.LENGTH_SHORT).show();
    }
}
private int buscarRegistro(int pos) {
    pos++;
    DBHelper helper = new DBHelper(this,"Persona",null,1);
    SQLiteDatabase db = helper.getReadableDatabase();



    String sql = "SELECT id, nombre, telefono, direccion from Persona WHERE ID="+ pos;
    Cursor c = db.rawQuery(sql,null);
    c.moveToFirst();
    Nombre.setText(c.getString(1));
    Direccion.setText(c.getString(2));
    Telefono.setText(c.getString(3));
    return pos;
}
private void eliminar(int pos) {

    int posicion = pos;
    DBHelper helper = new DBHelper(this, "Persona", null, 1);
    SQLiteDatabase db = helper.getWritableDatabase();
    try {
        String sql = "DELETE FROM Persona WHERE id ="+posicion;
        db.execSQL(sql);
        db.close();
        Toast.makeText(this, "Registro Eliminado", Toast.LENGTH_SHORT).show();
    }catch (Exception e){
        Toast.makeText(this, "Error: "+ e.getMessage(), Toast.LENGTH_SHORT).show();
    }

}
    
asked by German Silva 22.11.2017 в 09:03
source

1 answer

0

If you are using the position parameter that is obtained inside the Adapter, remember that this value is actually the index of the element inside the adapter, remember that the first index is always 0, for example:

position = 0 ; es el primer elemento.
position = 1 ; es el segundo elemento.
position = 2 ; es el tercer elemento.
position = 3 ; es el cuarto elemento.
etc....

Therefore, if you send the value of position, it may not match the id you have registered in your table, since the ids probably start from 1 and not from 0.

You can send the value of position and inside modify it so that it is the value of the id of the record by increasing the value by 1.

private void eliminar(int pos) {

    int posicion = pos + 1; //* Agrega +1 para que coincida con el id del elemento.

    DBHelper helper = new DBHelper(this, "Persona", null, 1);
    SQLiteDatabase db = helper.getWritableDatabase();
    try {
        String sql = "DELETE FROM Persona WHERE id ="+posicion;
        db.execSQL(sql);
        db.close();
        Toast.makeText(this, "Registro Eliminado", Toast.LENGTH_SHORT).show();
    }catch (Exception e){
        Toast.makeText(this, "Error: "+ e.getMessage(), Toast.LENGTH_SHORT).show();
    }

}

In fact, if you check your buscarRegistro() method, you are actually doing what I said earlier.

private int buscarRegistro(int pos) {

    pos++;  //*  Agrega +1 para que coincida con el id del elemento.

    DBHelper helper = new DBHelper(this,"Persona",null,1);
    SQLiteDatabase db = helper.getReadableDatabase();



    String sql = "SELECT id, nombre, telefono, direccion from Persona WHERE ID="+ pos;
    Cursor c = db.rawQuery(sql,null);
    c.moveToFirst();
    Nombre.setText(c.getString(1));
    Direccion.setText(c.getString(2));
    Telefono.setText(c.getString(3));
    return pos;
}
    
answered by 22.11.2017 / 18:03
source