英文:
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
推荐的更新表格的方法是使用带有ContentValues
的update()
方法。
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, "ID = ?", new String[] {"1"});
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论