Search code examples
androidsqlitedelete-record

Android delete Record SQLite


In my activity I have a spinner with data taken from SQLite db by this query:

private List<String> ottieniAnni(){
    List<String> result = new LinkedList<String>();

    SQLiteDatabase db = new BHelper(this).getReadableDatabase();

    String sql = "SELECT DISTINCT strftime('%Y',"+GTable.DATE+") FROM "+GTable.TABLE_NAME;
    Cursor c = db.rawQuery(sql, null);

    while (c.moveToNext()){
        result.add(c.getString(0));
    }
    db.close();
    return result;
}

Now through button, I want to delete all records in the year chosen by the user. I used the method db.delete (), but so will delete all the records. Should I set a parameter but do not know how to do it:

 SQLiteDatabase db= mHelper.getWritableDatabase();
    db.delete(GiornateTable.TABLE_NAME,   null, null );
    db.close();

Solution

  • Try like

    SQLiteDatabase db= mHelper.getWritableDatabase();
    int rowcount = db.delete(GiornateTable.TABLE_NAME,
                        "GIVE YOUR COLUMN NAME" + "=?",
                        new String[] { "GIVE YOUR VALUE" });
    Log.d("No of record deleted",String.valueOf(rowcount));