在Android中更新SQL表中的单个值。

huangapple go评论56阅读模式
英文:

Update single value in SQL table in Android

问题

我试图在一个只有1行1个表的SQL .db文件中更新一个值。不过,我只想改变CUSTOMER_ISACTIVE列。

    public static final String CUSTOMER_TABLE = "CUSTOMER_TABLE";
    public static final String COLUMN_CUSTOMER_EMAIL = "CUSTOMER_EMAIL";
    public static final String COLUMN_CUSTOMER_PASSWORD = "CUSTOMER_PASSWORD";
    public static final String COLUMN_CUSTOMRER_ISMEMBER = "CUSTOMRER_ISMEMBER";
    public static final String COLUMN_CUSTOMER_ISACTIVE = "CUSTOMER_ISACTIVE";
    public static final String COLUMN_CUSTOMER_PAYMENTERROR = "CUSTOMER_PAYMENTERROR";
    public static final String COLUMN_CUSTOMER_AUTOCARDTOKEN = "CUSTOMER_CARDTOKEN";

public void upDateActiveStatus(boolean isActive){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "UPDATE " + CUSTOMER_TABLE + " SET " + COLUMN_CUSTOMER_ISACTIVE  + "=" + (isActive ? 1 : 0) + " WHERE " + "ID = 1";
        db.execSQL(query);

但是这个代码并没有生效。因为当我稍后获取isActive的值时(我知道这部分是有效的),它没有被改变。有什么想法吗?

这是我的错误信息:java.lang.RuntimeException: android.database.sqlite.SQLiteException: no such column: true (code 1 SQLITE_ERROR): , while compiling: UPDATE CUSTOMER_TABLE SET CUSTOMER_ISACTIVE = true

英文:

I am trying to update a value in a SQL .db file that only has 1 row in 1 table. I just want to change the CUSTOMER_ISACTIVE column though.

    public static final String CUSTOMER_TABLE = "CUSTOMER_TABLE";
    public static final String COLUMN_CUSTOMER_EMAIL = "CUSTOMER_EMAIL";
    public static final String COLUMN_CUSTOMER_PASSWORD = "CUSTOMER_PASSWORD";
    public static final String COLUMN_CUSTOMRER_ISMEMBER = "CUSTOMRER_ISMEMBER";
    public static final String COLUMN_CUSTOMER_ISACTIVE = "CUSTOMER_ISACTIVE";
    public static final String COLUMN_CUSTOMER_PAYMENTERROR = "CUSTOMER_PAYMENTERROR";
    public static final String COLUMN_CUSTOMER_AUTOCARDTOKEN = "CUSTOMER_CARDTOKEN";

public void upDateActiveStatus(boolean isActive){
        SQLiteDatabase db = this.getWritableDatabase();
        String SS = "UPDATE " + CUSTOMER_TABLE + " SET " + COLUMN_CUSTOMER_ISACTIVE  + "=" + isActive + " WHERE " + "ID = 1";
        db.execSQL(SS);

But this is not working. Because when I pull the value of isActive later (I know this part works) It is unchanged.
Any ideas?

This was my error : java.lang.RuntimeException: android.database.sqlite.SQLiteException: no such column: true (code 1 SQLITE_ERROR): , while compiling: UPDATE CUSTOMER_TABLE SET CUSTOMER_ISACTIVE = true

答案1

得分: 1

推荐的更新表格的方法是使用带有ContentValuesupdate()方法。
update()方法还会返回受影响的行数,您可以进行检查。
因此,请按照以下方式执行:

public int upDateActiveStatus(boolean isActive){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_CUSTOMER_ISACTIVE, isActive ? 1 : 0);
    int rows = db.update(CUSTOMER_TABLE, cv, "ID = ?", new String[] {"1"});
    db.close();
    return rows;
}

您可以像这样调用该方法:

int rows = upDateActiveStatus(true);

然后检查变量rows的值,以获取更新的行数。

英文:

The recommended way to update the table is by using the method update() with ContentValues.<br/>
The method update() also returns the number of affected rows which you can check.<br/>
So do it like this:

public int upDateActiveStatus(boolean isActive){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_CUSTOMER_ISACTIVE, isActive ? 1 : 0);
    int rows = db.update(CUSTOMER_TABLE, cv, &quot;ID = ?&quot;, new String[] {&quot;1&quot;});
    db.close();
    return rows;
}

You can call the method like this:

int rows = upDateActiveStatus(true);

and then check the value of the variable rows to check the number of the updated rows.

huangapple
  • 本文由 发表于 2020年9月25日 02:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64052734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定