【发布时间】:2014-04-03 12:00:01
【问题描述】:
所以我试图通过传递指定行的 id 从我的数据库中删除一个行,我在网上找到了一些代码,但我真的不知道它是如何工作的。它使用 SQLiteDatabase 的 db.delete 方法中的 whereClause 参数。有人知道它背后的逻辑吗? getWhereClause 究竟做了什么?
//the delete method in my database class
public void deleteRow(String compareColumn, String compareValue) {
// ask the database manager to delete the row of given id
try {
String whereClause = getWhereClause(compareColumn, compareValue);
db.delete(TABLE_NAME, whereClause, null);
//db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);
} catch (Exception e) {
Log.e("DB DELETE ERROR", e.toString());
e.printStackTrace();
}
}
//the method that returns whereClause
private String getWhereClause(String compareColumn, String compareValue) {
String whereClause = null;
if (compareColumn == null || compareColumn == "") { }
else if (compareValue == null || compareColumn == "") { }
else { whereClause = compareColumn + "=\"" + compareValue + "\""; }
return whereClause;
【问题讨论】:
标签: java android database sqlite sql-delete