Honey H
Honey H

Reputation: 299

How to delete data from SQLite database from another class in android?

This is my first encounter with SQLite Database and I tried out this codes from http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

I have this in my Database Handler

// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
        new String[] { String.valueOf(contact.getID()) });
db.close();}

I wish to delete the data from another class (eg: when I click on a button in another java class, it will delete the data from the database.) How do I do that?

Upvotes: 0

Views: 2822

Answers (2)

Anant
Anant

Reputation: 153

Nice... but in delete() should be added, like so

public void delete()
    {   SQLiteDatabase db = this.getWritableDatabase();
    try {
        db.isOpen();           //should be added
    } catch (SQLException sqle) {
         Log.e("TAG", "Never ignore exception!!! " + sqle);
    } 
        db.delete(TABLE_Time, null, null);
        String Message = "Record is deleted: ";
    }

Upvotes: 0

Honey H
Honey H

Reputation: 299

I managed to solve it by placing this in Database Handler

public void delete()
{   SQLiteDatabase db = this.getWritableDatabase();
    db.delete(DATABASE_NAME, null, null);
}

and then call this in my ResetActivity class (a different class)

public void reset()
{
DBHandler database = new DBHandler(this);

try {
    database.delete();

} catch (NullPointerException e) {
    // TODO Auto-generated catch block
    Log.d("DATABASE", "ERROR!");
    e.printStackTrace();
}
}

Upvotes: 1

Related Questions