如何在android中使用"UPDATE Products SET Price = Price + 50 WHERE ProductID = 1"这种SQlite语句?
How to use "UPDATE Products SET Price = Price + 50 WHERE ProductID = 1" this kind of SQlite statement in android?
我想更新 Column.I 的特定行试图使用语句使用 'db.update()' 设置我的值。但是我 cant.Is 有任何我可以使用的方法使用题目的语句设置特定行某列的值?
要更新您的 sql 数据库,请使用 db.update(TABLE_NAME, values, where, whereArgs);
您可以阅读 here。用例可能如下所示:
ContentValues cv = new ContentValues();
cv.put("column_name", "updated value");
db.update("table_name", cv, "column_id" + "=?",new String[]{"primary_id"});
update()
仅适用于 ContentValues
,它是 SQL 变量绑定的包装器。您只能绑定值,不能绑定像 Price + 50
.
这样的表达式
要使用这样的表达式,请将 execSQL()
与您已有的原始 SQL 字符串一起使用。
尽管文档明确提到更新 table 的推荐和正确方法是 update()
方法,但您的要求只能通过 execSQL()
.[=24= 完成]
db.execSQL("UPDATE Products SET Price = Price + 50 WHERE ProductID = 1");
或 execSQL (String sql, Object[] bindArgs)
:
db.execSQL(
"UPDATE Products SET Price = Price + ? WHERE ProductID = ?",
new String[] {"50", "1"}
);
这可能对您有所帮助。 db.update() 和 ContentValues () 是更安全的更新方法。检查这个。
int i = 50;
Cursor res = db.rawQuery("select * from Products where ProductId = 1 LIMIT 1");
if(res.getCount() > 0){
i += Integer.parseInt(res.getString(res.getColumnIndex("Price")));
}
ContentValues values = new ContentValues();
values.put("Price", i.toString());
db.update("Products",values,"ProductId = ?",new String[]{"1"});
我不确定,但我认为问题可能在于将价格列类型设置为文本。如果是这样,请将其更改为整数。
你也可以试试
db.execSQL( "UPDATE Products SET Price = ? WHERE ProductID = ?", new String[] {Price+50 , "1"} );
我想更新 Column.I 的特定行试图使用语句使用 'db.update()' 设置我的值。但是我 cant.Is 有任何我可以使用的方法使用题目的语句设置特定行某列的值?
要更新您的 sql 数据库,请使用 db.update(TABLE_NAME, values, where, whereArgs);
您可以阅读 here。用例可能如下所示:
ContentValues cv = new ContentValues();
cv.put("column_name", "updated value");
db.update("table_name", cv, "column_id" + "=?",new String[]{"primary_id"});
update()
仅适用于 ContentValues
,它是 SQL 变量绑定的包装器。您只能绑定值,不能绑定像 Price + 50
.
要使用这样的表达式,请将 execSQL()
与您已有的原始 SQL 字符串一起使用。
尽管文档明确提到更新 table 的推荐和正确方法是 update()
方法,但您的要求只能通过 execSQL()
.[=24= 完成]
db.execSQL("UPDATE Products SET Price = Price + 50 WHERE ProductID = 1");
或 execSQL (String sql, Object[] bindArgs)
:
db.execSQL(
"UPDATE Products SET Price = Price + ? WHERE ProductID = ?",
new String[] {"50", "1"}
);
这可能对您有所帮助。 db.update() 和 ContentValues () 是更安全的更新方法。检查这个。
int i = 50;
Cursor res = db.rawQuery("select * from Products where ProductId = 1 LIMIT 1");
if(res.getCount() > 0){
i += Integer.parseInt(res.getString(res.getColumnIndex("Price")));
}
ContentValues values = new ContentValues();
values.put("Price", i.toString());
db.update("Products",values,"ProductId = ?",new String[]{"1"});
我不确定,但我认为问题可能在于将价格列类型设置为文本。如果是这样,请将其更改为整数。
你也可以试试
db.execSQL( "UPDATE Products SET Price = ? WHERE ProductID = ?", new String[] {Price+50 , "1"} );