android - 如何使用where参数更新和删除SQLite数据库中的一行?

时间:2016-06-22 13:00:35

标签: android sqlite

所以我试图在我的应用程序中使用db.update和db.delete的where args从我的SQLite数据库中只删除一个值实例,但它似乎删除并更新了所有匹配的值。我只想更新或删除一个。我该怎么做?我尝试使用游标,但我的实现似乎是错误的。帮助

public long updateData(String oldFood, String oldCalorie, String newFood, Float newCalorie, String newDate) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(helper.FOOD_COLUMN, newFood);
    contentValues.put(helper.CALORIE_COLUMN, newCalorie);
    contentValues.put(helper.DATE, newDate);
    String[] columns = {helper.UID, helper.FOOD_COLUMN, helper.CALORIE_COLUMN, helper.DATE};
    //long id = db.update(helper.TABLE_NAME, contentValues, helper.FOOD_COLUMN + " = ? AND " + helper.CALORIE_COLUMN + " = ?", new String[]{oldFood, oldCalorie});
    Cursor cursor = db.query(helper.TABLE_NAME, columns, null, null, null, null, null);
    Information current = new Information();
    int uidIndex = cursor.getColumnIndex(helper.UID);
    int cid = cursor.getInt(uidIndex);
    int foodIndex = cursor.getColumnIndex(helper.FOOD_COLUMN);
    int calorieIndex = cursor.getColumnIndex(helper.CALORIE_COLUMN);
    float oldCalorieFloat = Float.parseFloat(oldCalorie);
    if (oldFood == cursor.getString(foodIndex) && oldCalorieFloat == cursor.getFloat(calorieIndex)) {
        long id = db.update(helper.TABLE_NAME, contentValues, helper.FOOD_COLUMN + " = ? AND " + helper.CALORIE_COLUMN + " = ?", new String[]{oldFood, oldCalorie});
        db.close();
        return id;
    }
    long id=0;
    return id;
}

0 个答案:

没有答案