我目前正致力于在Android数据库中添加,查看和删除值,但遗憾的是我只提供了添加和查看部分的书,所以我来这里寻求帮助。
我有一个名为DeleteEntry的类,您输入一个变量moduleDelete,它将传递给类DeleteEntryViewModule。然后,此类使用此值使用此showEvent()方法返回该行上的所有值
private void showEvents(Cursor cursor) {
StringBuilder builder = new StringBuilder("Saved Events:\n");
while (cursor.moveToNext()){
long id = cursor.getLong(0);
String module = cursor.getString(1);
String day = cursor.getString(2);
String starttime = cursor.getString(3);
String duration = cursor.getString(4);
String typeofsession = cursor.getString(5);
String room = cursor.getString(6);
builder.append(id).append(": ");
builder.append(module).append(": ");
builder.append(day).append(": ");
builder.append(starttime).append(": ");
builder.append(duration).append(": ");
builder.append(typeofsession).append(": ");
builder.append(room).append("\n ");
}
TextView text = (TextView) findViewById(R.id.text);
text.setText(builder);
}
如何从构建器中获取值并将它们放入变量中,以便我可以执行此代码。
private void removeEvent (String module, String day, String starttime, String duration, String typeofsession, String room){
try{
SQLiteDatabase db = events.getWritableDatabase();
ContentValues values = new ContentValues ();
values.put(DAY, day);
values.put(MODULE, module);
values.put(STARTTIME, starttime);
values.put(DURATION, duration);
values.put(TYPEOFSESSION, typeofsession);
values.put(ROOM, room);
db.replaceOrThrow(TABLE_NAME, null, values);
}catch(SQLiteException ex){
Toast toast = Toast.makeText(this, "Broken", 10);
toast.show();
}
}
由于
答案 0 :(得分:0)
尝试使用以下代码而不是db.replaceOrThrow
return db.delete(TABLE_NAME, MODULE + "="
+ module, null) > 0;
您能解释为什么使用db.replaceOrThrow ??