Delete button [duplicate]

2
Good afternoon, I made a delete method in my bank and called it in my activity, but my problem is that when I click the button it does not happen, and it does not show me any errors in LogCat, someone would know how to help me ?

DbHelper:

package Base;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

public class DbHelper extends SQLiteOpenHelper {

    private static final String NAME_BASE = "Resultados";
    private static final int VERSION_BASE = 1;

    public DbHelper(Context context) {

        super(context, NAME_BASE, null, VERSION_BASE);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sqlCreateTableResultado = "CREATE TABLE resultado("
                + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "TimeCasa TEXT,"
                + "TimeFora TEXT,"
                + "GolsCasa INTEGER,"
                + "GolsFora INTEGER"+")";

        db.execSQL(sqlCreateTableResultado);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sqlDropTableResultado = "DROP TABLE resultado";

        db.execSQL(sqlDropTableResultado);

        onCreate(db);

    }

    public void insertResultado(Esporte resultado){
        SQLiteDatabase db = getWritableDatabase();

        ContentValues valores = new ContentValues();
        valores.put("TimeCasa", resultado.getNomeTimeUm());
        valores.put("TimeFora", resultado.getNomeTimeDois());
        valores.put("GolsCasa", resultado.getValorUm());
        valores.put("GolsFora", resultado.getValorDois());

        db.insert("resultado", null, valores);

        db.close();
    }

    public List<Esporte> selectTodosResult(){
        List<Esporte> listResult = new ArrayList<Esporte>();
        SQLiteDatabase db = getReadableDatabase();

        String sqlSelectTodosResult = "SELECT * FROM resultado";

        Cursor c = db.rawQuery(sqlSelectTodosResult, null);

        if (c.moveToFirst()){
            do {
                Esporte onde = new Esporte();
                onde.setId(c.getInt(0));
                onde.setNomeTimeUm(c.getString(1));
                onde.setNomeTimeDois(c.getString(2));
                onde.setValorUm(c.getInt(3));
                onde.setValorDois(c.getInt(4));

                listResult.add(onde);
            }
            while (c.moveToNext());
        }

        db.close();
        return listResult;
     }

    public void delete(){
        SQLiteDatabase db = getReadableDatabase();
        String sqlSelectTodosResult = "DELETE * FROM resultado";
        Cursor c = db.rawQuery(sqlSelectTodosResult, null);
    }

}

My Activity:

package com.allsport.miyonic.allsport;

import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.ListView;
        import java.util.List;

        import Base.DbHelper;
        import Base.Esporte;

        import static android.os.FileObserver.DELETE;

public class ResultSimples extends AppCompatActivity {

    private ListView lista;
    private Button apagar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_simples);

        lista = (ListView) findViewById(R.id.ListaTimes);
        apagar = (Button) findViewById(R.id.btndeletar);

        apagar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbHelper dd = new DbHelper(ResultSimples.this);
                dd.delete(); // É necessário passar o parâmetro
            }
        });
    }

    @Override
    public void onResume(){
        super.onResume();

        DbHelper dbhe = new DbHelper(this);
        List<Esporte> listaResultPartida = dbhe.selectTodosResult();

        ArrayAdapter<Esporte> adp = new ArrayAdapter<Esporte>(this, android.R.layout.simple_list_item_1, listaResultPartida);

        lista.setAdapter(adp);
    }


}

Thank you ...

    
asked by anonymous 31.10.2016 / 19:02

2 answers

5

First, d.delete("resultado", "id = " + id, null); is a security fault . You are allowing an attack by calling SQL Injection. This third parameter exists precisely for this, to avoid SQL Injection. Even though in this particular case it is impossible to practice an injection, it is better to take care and not get used to writing SQL sentences in this way.

So this code should be

public void delete(long id){
    SQLiteDatabase d = getWritableDatabase();
    if (id > -1) {
        String[] whereArgs = new String[] { String.valueOf(id) };
        d.delete("resultado", "id=?", whereArgs);        
    }
    d.close();
}

The problem pointed out in the question is that the method delete within DbHelper , asks as a parameter long and nothing is being passed

apagar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DbHelper dd = new DbHelper(ResultSimples.this);
        dd.delete(PARAMETRO_AQUI); // É necessário passar o parâmetro
    }
});
    
31.10.2016 / 19:25
3

The error is in the parameter that the delete method is receiving

public void delete(long id){
    SQLiteDatabase d = getWritableDatabase();
    if (id > -1) {
        d.delete("resultado", "id = " + id, null);
    }
    d.close();
}

It is waiting for a long and you are not passing anything, this generates the error.

At the time of calling

dd.delete();

You must pass this id together or remove this parameter there from the method.

Update:

Leave your method that way

public void delete(){
   SQLiteDatabase d = getWritableDatabase();
   d.delete("resultado", null, null);
   d.close();
}

And then you can call the

dd.delete();
    
31.10.2016 / 19:11