【问题标题】:Problem with updating data into SQLite-android将数据更新到 SQLite-android 的问题
【发布时间】:2020-09-14 13:51:36
【问题描述】:

这是我的桌子

db.execSQL("CREATE TABLE tbl_groupAccount ( \n" +
        "    ID                 INTEGER PRIMARY KEY AUTOINCREMENT\n" +
        "                               NOT NULL\n" +
        "                               UNIQUE,\n" +
        "    ACCOUNT_GROUP_NAME VARCHAR,\n" +
        "    ACCOUNT_GROUP_TYPE  VARCHAR \n" +
        ");");

我用它来将数据更新到表中

public boolean updateAccountGroup(String nameAccountGroup,String groupAccountType, Integer id) {
    boolean result;
    String sql = "UPDATE tbl_groupAccount" +
            " SET ACCOUNT_GROUP_NAME , ACCOUNT_GROUP_TYPE = '" + nameAccountGroup + groupAccountType + "'" +
            "WHERE ID = " + id;

    try {
        SQLiteDatabase database = this.getWritableDatabase();
        database.execSQL(sql);
        database.close();
        result = true;
    } catch (Exception ex) {
        result = false;
    }
    return result;
}

当我单击按钮将数据更新到表中时,没有任何反应。并且没有错误发生。并且没有任何内容更新到表中。有人可以帮助我。请

谢谢

【问题讨论】:

    标签: android sqlite sql-update android-sqlite


    【解决方案1】:

    你的更新 sql 应该是这样的:

    String sql = "UPDATE tbl_groupAccount" +
                    " SET ACCOUNT_GROUP_NAME = '" + nameAccountGroup + "' , ACCOUNT_GROUP_TYPE ='"+ groupAccountType + "'" +
                    " WHERE ID = " + id;
    

    【讨论】:

      【解决方案2】:

      UPDATE 语句的语法错误。
      也不要连接要传递给查询的参数。
      推荐且安全的方法是使用 update()? 占位符方法:

      public boolean updateAccountGroup(String nameAccountGroup,String groupAccountType, Integer id) {
          ContentValues c = new ContentValues();
          c.put("ACCOUNT_GROUP_NAME", nameAccountGroup);
          c.put("ACCOUNT_GROUP_TYPE", groupAccountType);
          SQLiteDatabase db = this.getWritableDatabase();
          boolean result = db.update("tbl_groupAccount", c, "ID = ?", new String[] {String.valueOf(id)}) > 0;
          db.close();            
          return result;
      }
      

      update() 方法返回受影响/更新的行数,因此如果此数字大于0updateAccountGroup() 方法将返回true

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-21
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多