【问题标题】:SQLite Android - Updating (adding up) existing value with new oneSQLite Android - 用新值更新(加起来)现有值
【发布时间】:2015-08-06 14:44:34
【问题描述】:

我是 android 新手,我正在尝试使用这些代码来更新条目的值:

"Update " + TableIncomeCategory + " Set " + Income_Cat_currentAmount + " = " + "'" + Income_Cat_currentAmount + "'"
                        + "+" + "'"+tran.getAmount()+"'" + " where " + Income_category_name + " LIKE " + "'" + tran.getCategory() + "'");

我正在尝试将当前金额 (Income_Cat_currentAmount) 与新值 (tran.getAmount()) 相加一起。谁能告诉我为什么以及如何解决它?非常感谢!!

【问题讨论】:

    标签: android database sqlite


    【解决方案1】:

    您的问题是格式。 SQL 命令最终是这样的:

    Update IncomeCategoryTab
    Set currentAmount = 'currentAmount'+'123'
    where category_name LIKE 'some category'
    

    这是错误的,因为您将一个字符串附加到另一个字符串。

    你不能引用列名,你不应该引用不是字符串的值:

    Update IncomeCategoryTab
    Set currentAmount = currentAmount + 123
    where category_name LIKE 'some category';
    

    在 Java 中,您应该为字符串值(如类别名称)使用参数以避免必须正确引用它们(如果它们可以包含引号,这将变得更加复杂):

    db.execSQL("Update "+TableIncomeCategory+
               " Set "+Income_Cat_currentAmount+" = "+Income_Cat_currentAmount+" + "+tran.getAmount()+
               " where "+Income_category_name+" LIKE ?;",
               new Object[]{ tran.getCategory() });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      相关资源
      最近更新 更多