Search code examples
androidsqlitesql-updateandroid-sqlite

Problem with updating data into SQLite-android


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

and I use this to update data into the 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;
}

When I click on the button to update data into the table, nothing happens. And no errors occur. And nothing is updated to the table. Someone can help me. Please

Thanks


Solution

  • Your update sql should be like:

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