如何删除SQLite数据库中的条目?

时间:2019-07-03 05:09:17

标签: android android-sqlite

我试图制作DeleteEvent(),但是它不起作用。我需要在哪里更改代码?

这是一个Android Studio Java代码。

public class DBStructure {enter code here
    public static final String DB_NAME =    "EVENTS_DB";
    public static final int DB_VERSION = 1;
    public static final String EVENT_TALE_NAME = "eventstable";
    public static final String EVENT = "event";
        public static final String TIME = "time";
    public static final String DATE = "date";
    public static final String MONTH = "month";
    public static final String YEAR = "year";

}

这是我的DeleteEvent()

public void DeleteEvent(String event, String time, String date, String month, String year, SQLiteDatabase database){
    ContentValues contentValues = new ContentValues();
    contentValues.remove(DBStructure.EVENT);
    contentValues.remove(DBStructure.TIME);
    contentValues.remove(DBStructure.DATE);
    contentValues.remove(DBStructure.MONTH);
    contentValues.remove(DBStructure.YEAR);
    database.delete(DBStructure.EVENT_TALE_NAME, "?=? AND ?=?",
            new String[] {DBStructure.EVENT, event, DBStructure.TIME, time});
}

1 个答案:

答案 0 :(得分:1)

您不需要ContentValues即可使用delete()方法:

public void DeleteEvent(String event, String time, String date, String month, String year, SQLiteDatabase database){
    database.delete(
        DBStructure.EVENT_TALE_NAME, 
        DBStructure.EVENT + " = ? AND " + DBStructure.TIME + " = ? AND " + DBStructure.DATE + " = ? AND " + 
        DBStructure.MONTH + " = ? AND " + DBStructure.YEAR + " = ?",
        new String[] {event, time, date, month, year}
    );
}

在第二个参数中,您构造WHERE子句,而3d则以字符串数组的形式包含将替换占位符?的参数。