0
I would like to delete not only a table but the database as a whole in Sqlite. Searching, I was told of a method Context.deleteDatabase()
, but I can’t use, if I try to call in a method, it says it’s not static to be used in a static class, and I don’t know how to instantiate a "context" object to use that 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 to the bank:
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";
}
}
Can anyone 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 program.
To delete the database, you also need to pass the database extension. Example:
ctx.deleteDatabase("databasename.db");
– viana
this "ctx".deleteDatabase... is a right context? How do I create the instance of this context to use the Contexte class method (deleteDatabase)???
– Adson Lucas Silva de Farias
When you use the method
deletarUsuario
, you obviously depending on theactivity
that you are, it should be something likedeletarUsuario(this, "databasename.db")
– viana
I tried using getBaseContext but it didn’t work, I’ll try as you say. Thank you!
– Adson Lucas Silva de Farias
If you’re doing it in a
fragment
, then you instead of usingthis
, putsgetActivity()
. Thus:deletarUsuario(getActivity(), "databasename.db")
– viana
It’s not a Fragment but it was good to know, so test warning here if it worked out thank you!
– Adson Lucas Silva de Farias
@Adsonlucassilvadefarias The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero