How to delete not only tables but a database in SQLite

0

I would like to delete not only a table but the whole database in SQLite. Searching, I was told of a Context.deleteDatabase() method, but I can not use it, if I try to call a method, it says it is not static to be used in a static class, and I do not know how to instantiate a context object to use this function.

This is my Helper class:

public class bdHelperUser extends SQLiteOpenHelper {
    private static final String NOME_BD = "user.db";
    private static final int VERSAO_BD = 1;

    public bdHelperUser(Context ctx){
        super(ctx, NOME_BD, null, VERSAO_BD);//o terceiro item é um cursor factor (não obrigatório)
    }

    @Override
    public void onCreate(SQLiteDatabase sqlite) {
        //tabela principal de dados dos usuários
        String sql = "create table if not exists usuario (_id integer primary key autoincrement, " +
                "nome text not null, endereco text not null, senha text not null, " +
                "mail text not null, cpf text not null, telefone text not null, " +
                "sexo text not null, dt_nascimento text not null, auto_login integer, " +
                "pin number(4), foto blob, apelido text);";

        sqlite.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //a cada versão nova do banco o script de mudança deve ser inserido


    }
}

This is the method I want to delete everything from the database:

public void deletarConta(View v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle(getString(R.string.deletar_conta_titulo));

        builder.setIcon(R.drawable.del);

        builder.setMessage(getString(R.string.deletar_conta_confima));

        builder.setPositiveButton(R.string.confirma, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
               String resposta =  crud.deletarUsuario(,"user.db");//aqui ocorre o erro
               exibirMsg(resposta);
                finish();
                login();
            }
        });

        builder.setNegativeButton(R.string.cancela, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {

            }
        });

        alerta = builder.create();

        alerta.show();
    }

And this is the method that deletes data in the database:

public String deletarUsuario(Context ctx, String strg){

        try {
            sqLite = helper.getWritableDatabase();
            sqLite.delete("usuario", null, null);
            //sqLite.deleteDatabase(file);
            //sqLite.execSQL("DROP TABLE IF EXISTS visitas");
            sqLite.close();
            ctx.deleteDatabase(strg);//aqui não consigo chamar esse método na activity pois não sei como criar um objeto Context pra passar como parâmetro e nem posso chamar esse método diretamente na classe

            return "Deletado com sucesso";
        }catch (SQLException erro){
           erro.getMessage();
            return "erro ao deletar";
        }
    }

Someone can help me by launching an example of how to do this via API, I know that if I delete the APP and reinstall it works but wanted to know how to do it programatically.

    
asked by anonymous 31.08.2017 / 01:28

1 answer

1

I believe this should work:

sqlite.deleteDatabase("nome do banco");

Or this:

ctx.deleteDatabase("nome do banco");
    
31.08.2017 / 01:34