How to delete all records from the database

Asked

Viewed 2,189 times

0

Good afternoon, in my application has a database that shows the results of the game, I put a button that its function will be to erase all the data of the bank... how do I do this part? in the blogs did not understand very well...

my get and set:

package Base;
public class Esporte {

    private int id;
    private String nomeTimeUm;
    private String nomeTimeDois;
    private int valorUm;
    private int valorDois;

    public Esporte(){}

    public Esporte(int id, String nomeTimeUm, String nomeTimeDois, int valorUm, int valorDois){
        this.id = id;
        this.nomeTimeUm = nomeTimeUm;
        this.nomeTimeDois = nomeTimeDois;
        this.valorUm = valorUm;
        this.valorDois = valorDois;
    }

    public int getId(){return id;}

    public void setId(int id){this.id = id;}

    public String getNomeTimeUm(){return nomeTimeUm;}

    public void setNomeTimeUm(String nomeTimeUm){this.nomeTimeUm = nomeTimeUm;}

    public String getNomeTimeDois(){return nomeTimeDois;}

    public void setNomeTimeDois(String nomeTimeDois){this.nomeTimeDois = nomeTimeDois;}

    public int getValorUm(){return valorUm;}

    public void setValorUm(int valorUm){this.valorUm = valorUm;}

    public int getValorDois(){return valorDois;}

    public void setValorDois(int valorDois){this.valorDois = valorDois;}

    @Override
    public String toString(){
        return nomeTimeUm+ " X " +nomeTimeDois+
                " = " +valorUm+ " X " +valorDois;
    }

}

my 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;
     }
}

Class da Activity:

package com.allsport.miyonic.allsport;

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

import Base.DbHelper;
import Base.Esporte;

public class ResultSimples extends AppCompatActivity {

    private ListView lista;

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

        lista = (ListView) findViewById(R.id.ListaTimes);
    }

    @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);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.allsport.miyonic.allsport.ResultadoSimples"
    android:background="#003366">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/ListaTimes"
        android:textColor="#fff"
        android:layout_marginTop="90dp" />

    <TextView
        android:text="Resultado das Partidas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100px"
        android:id="@+id/textView"
        android:textColor="#fff"
        android:textSize="29dp"
        android:textStyle="normal|italic"
        android:textAlignment="center" />

    <Button
        android:text="Deletar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btndeletar"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="11dp"/>

</RelativeLayout>

Thank you...

2 answers

3


You can use an SQL query as Jonatas Nardi mentioned or use Sqlite-specific commands.

Also, there is difference between deleting lines or dropping the table completely.

The method for delete is implemented in Dbhelper:

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

Or, using SQL query:

public void deletar(){
     SQLiteDatabase db = getWritableDatabase();
     db.execSQL(DELETE FROM resultado);
     db.close();
}

The method for drop would be as follows:

public void dropar(){
     SQLiteDatabase db = getWritableDatabase();
     db.execSQL(DROP TABLE IF EXISTS resultado);
     db.close();
}

I believe that to invoke these methods from onClick, you will need to instantiate a Dbhelper within onClick itself or make Dbhelper an attribute of Activity (the way the code is, Dbhelper is instantiated only in onResume().

0

If you want to delete all records from a table in a database, you can use the following syntax:

DELETE FROM NomeDaTabela;

Hugs!

  • would look like this then: public delete(){ DELETE FROM result} and in the onclick method put as?

  • This is the query to delete in SQL database

  • Where I put with onclick function?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.