gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:02:03 25 4
gpt4 key购买 nike

我尝试创建 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});
}

最佳答案

您不需要 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 以字符串数组的形式包含将替换占位符 ? 的参数。

关于java - 如何删除 SQLite 数据库中的条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56863242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com