不更新数据库中的值而是插入新条目

时间:2017-06-07 11:01:26

标签: android android-sqlite

我使用下面的代码更新数据库并将where子句放入日期

 ContentValues values = new ContentValues();
    values.put(DBTransactions.USER_ID_KEY, userId);
    values.put(DBTransactions.DATE_KEY, date);
    values.put(DBTransactions.DAY_KEY, day);
    values.put(DBTransactions.GOAL_KEY, dailyGoal);
    values.put(DBTransactions.VOLUME_KEY, dailyVolume);
    values.put(DBTransactions.IS_GOAL_MET_KEY, goalMet);
    db.getWritableDatabase().update(DBTransactions.TRANSACTION_TABLE_NAME, values, DBTransactions.DATE_KEY + " = ?", new String[]{date});
    closeDB();

我在把where子句放在哪里错了?它不是更新数据库,它在数据库中创建新条目。

它没有更新数据库。 在此先感谢

3 个答案:

答案 0 :(得分:0)

我正在使用此代码更新数据库,它的工作正常,不是我的问题。

  public void updateItemToCartTable(String itemId, String subitemid, String cost, int quantity) {

    sdb = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(ITEM_COST, cost);
    values.put(ITEM_QUANTITY, quantity);

    Log.d("DB", "Count : " + quantity);

    this.sdb.update(TABLE_NAME, values, ITEM_ID + " ='" + itemId + "'         AND " + SUB_ITEM_ID + " ='" + subitemid + "'", null);
    sdb.close();
}

使用此代码更新数据库

答案 1 :(得分:0)

使用以下代码更新您的表格:

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(DBTransactions.USER_ID_KEY, userId);
values.put(DBTransactions.DATE_KEY, date);
values.put(DBTransactions.DAY_KEY, day);
values.put(DBTransactions.GOAL_KEY, dailyGoal);
values.put(DBTransactions.VOLUME_KEY, dailyVolume);
values.put(DBTransactions.IS_GOAL_MET_KEY, goalMet);

db.update(DBTransactions.TRANSACTION_TABLE_NAME, values, DBTransactions.DATE_KEY + " = ? ", new String[]{String.valueOf(date)});

closeDB();

答案 2 :(得分:0)

  • DBTransactions.TRANSACTION_TABLE_NAME表格中DBTransactions.DATE_KEY列的数据类型是什么?
  • 在本地数据库中插入数据,并检查DBTransactions.TRANSACTION_TABLE_NAME表中插入日期的格式。
  • 在根据DBTransactions.DATE_KEY条件更新记录时,请检查DBTransactions.DATE_KEY是否与DBTransactions.TRANSACTION_TABLE_NAME DBTransactions.DATE_KEY列中存储的值相匹配。

您的代码中的其他所有内容似乎都很好。

相关问题