将数据更新到 SQLite 时出现问题-android

Problem with updating data into SQLite-android

这是我的table

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" +
        ");");

我用它来更新数据到 table

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;
}

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

谢谢

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

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

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() return 是 affected/updated 行的数量,所以如果这个数字大于 0 方法 updateAccountGroup() 将 return true.