简体   繁体   中英

Delete specific record in sqlite table based on two criteria: _id and column

I have created a sqlite table for my android app, this table has 5 columns and multiple rows, the columns being: _id, column1, column2, column3, column4.

I want to delete a specific record, for instance the record stored in column3 corresponding to _id (in a different class are the getters and setters, for this I've named the class "TableHandler")

I guess that I'm a bit confused, following is what I was planning, but for column3 I'm not sure what should be the argument, I just want to delete whatever is in that column position corresponding to _id

 public void deleteValueColumn3(TableHandler value){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, KEY_ID + " = ? AND " + KEY_COLUMN3 + " = ?",
                new String[] {String.valueOf(value.getID()), ?????????);
        db.close();

    }

The ???????? is that I'm stuck there, maybe the whole method needs to be rewritten, I would appreciate your input.

Thanks

If you want to delete the whole record, just use the _id of the record in delete method, because that is the primary key for your table and therefore is unique. If you'd rather keep the record, you con always use the SQLiteDatabase.update method, specifying null as the new value that will replace column3 value; check out that column3 declaration has no NOT NULL tag, otherwise that could easily throw exception at you.

SQLite does not allow you to delete columns for a specific row. You can only delete ROWS of data (delete the row that has the column _ID = 1).

Here 'sa quick tutorial on SQL .

How about updating that column with a null value, rather than using delete() ?

ContentValues cv = new ContentValues();
cv.putNull(KEY_COLUMN3);

db.getWritableDatabase().update(
        TABLE_NAME,
        cv,
        KEY_ID + "=?",
        new String[]{String.valueOf(keyIdValue)});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM