Delete data from Sqlite database

Asked

Viewed 1,147 times

1

I’m making a database program. I wish I had the option to delete a data from database Sqlite. So here’s what I did:

In my OpenHelper called the following method:

public void delete (){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM CLIENTE");
    db.close();
}

Where CLIENT is the name of my table. Hence I created the method:

public void deletar (View view){
    Dados_familiaOpenHelper dados_familiaOpenHelper = new Dados_familiaOpenHelper(null);
    dados_familiaOpenHelper.delete();

}

This method is in a activity that contains a button. This button onClick and redirects to this method.

Dai ok. When I run my program, and click on the button, the app closes. Anyone has any advice or anything that can help me?

@EDIT

My Openhelper class:

public class Dados_familiaOpenHelper extends SQLiteOpenHelper {

public Dados_familiaOpenHelper(Context context){
    super(context, "Dados_familia", null, 4);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(ScriptDLL.getCreateTableCliente() );

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void delete (){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM CLIENTE");
    db.close();
}
  • Only with this information is not possible to know for sure, but the cause may be due to the passing null to the builder of Dados_familiaOpenHelper.

  • I’m half lost, still learning. What I should pass as a builder in Dados_familiaopenhelper?

  • Put the question to the class Dados_familiaOpenHelper

  • I put the Dados_familiaopenhelper class.

  • Anyone? Any ideas? kkk

  • Error appears in logcat?

Show 1 more comment

1 answer

0

I do not know if it is still useful, but it follows a solution:

public void delete(int id)
{
    try
    {
        String[] args = {id};
        getWritableDatabase().delete("Dados_familia", "id = ?", args);
        getWritableDatabase().close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

NOTE: It is worth remembering that "Dados_familia" is the name of your database table, so that is where the actions should be performed and not in the table "client", as it is in your delete method().

Browser other questions tagged

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