How to erase database data in android studio?

2

I have an application where the user logs in the first time, I save Token and NIU , so the next time he accesses the application, he already goes directly to Webview using this token e NIU who are in the bank. However, I need to delete this data when the user clicks the "Exit" button on webview . I already have a treatment for this button (exit), I can enter the database erase treatment here too, how to do it?

BT_SAIR METHOD:

     private void SairWV(){
     Intent intent = new Intent(Webview.this, MainActivity.class);
     startActivity(intent);
     finish();

METHOD TO INSERT THE BANK:

    public long insere (Token token){
    dao = new BancoDeDados(context);
    SQLiteDatabase db = dao.getWritableDatabase(); 
    dao.onCreate(db);
    ContentValues dados = pegaDadosDoAcesso(token); 
    long inserir = db.insert(NOMETABELA, null, dados);
    db.close();
    Log.i(NOMETABELA, inserir + "");
    return inserir;
}
    
asked by anonymous 21.02.2017 / 14:00

3 answers

2

To delete in your database you follow + - the same idea that you are already using:

db.delete(TABELA, CHAVE + "=" + chave, null)

Where TABELA is the table you want to perform Delete, CHAVE is the identifier that you use for that row you want to delete (either a primary key, a name, a date, whatever you want), and chave is the value to be compared to delete.

This returns the number of rows that have been deleted, so you can always test if they are > 0 to see if it really deleted.

It's worth checking the documentation :

    
21.02.2017 / 14:03
2

To delete all data use:

db.execSQL("DELETE FROM " + NOMETABELA);

To delete just one row from the table:

db.delete(NOMETABELA, "NOMECAMPO" + "='" + VALOR + "'", null);
    
21.02.2017 / 14:05
0

Here's an example:

   private void SairWV(){

        dao = new BancoDeDados(context);
        SQLiteDatabase db = dao.getWritableDatabase();
        /** irá remover todos os registros **/
         db.execSQL(String.format("DELETE FROM %s", NOMETABELA));
        /**
         * Quando se usa DELETE TABLE
         para excluir todos os registros,
         recomenda-se usar o comando VACUUM para
         limpar o espaço não utilizado.
         **/
        db.execSQL("VACUUM");
        Intent intent = new Intent(Webview.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

source

    
21.02.2017 / 14:06