How to use "UPDATE Products SET Price = Price + 50 WHERE ProductID = 1" this kind of SQlite statement in android?

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

How to use "UPDATE Products SET Price = Price + 50 WHERE ProductID = 1" this kind of SQlite statement in android?

问题

我想更新特定行的列。我尝试使用语句使用'db.update()'来设置我的值。但是我无法成功。是否有任何方法可以使用标题的语句来设置列的特定行的值?

英文:

I want to update the a specific row of the Column.I tried to use the statement to set my value using 'db.update()'.But I cant.Is there any method which I can use to use the statement of the title to set the value of a specific row of a column?

答案1

得分: 1

尽管文档明确提到更新表的推荐和适当方式是使用 update() 方法,但您的要求只能通过 execSQL() 方法完成。<br/>

可以使用 execSQL(String sql) 方法之一:

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"}
);
英文:

Although the documentation explicitly mentions that the recommended and proper way to update a table is the method update(), your requirement can only be done with execSQL().<br/>

Either with execSQL(String sql):

db.execSQL(&quot;UPDATE Products SET Price = Price + 50 WHERE ProductID = 1&quot;);

or with execSQL (String sql, Object[] bindArgs):

db.execSQL(
    &quot;UPDATE Products SET Price = Price + ? WHERE ProductID = ?&quot;,
    new String[] {&quot;50&quot;, &quot;1&quot;}
);

答案2

得分: 1

这可能对您有所帮助。使用 ContentValues() 的 db.update() 是更安全的更新方法。请检查这个方法。

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", Integer.toString(i));
db.update("Products", values, "ProductId = ?", new String[]{"1"});

我不确定,但我认为问题可能是将 Price 列的数据类型设置为文本。如果是这样,请将其更改为整数。

英文:

This may help you. db.update() with ContentValues () is more safe updating method. Check this one.

     int i = 50;
     Cursor res = db.rawQuery(&quot;select * from Products where ProductId = 1 LIMIT 1&quot;);
      if(res.getCount() &gt; 0){
            i += Integer.parseInt(res.getString(res.getColumnIndex(&quot;Price&quot;)));
        }
    
     ContentValues values = new ContentValues();
     values.put(&quot;Price&quot;, i.toString());
     db.update(&quot;Products&quot;,values,&quot;ProductId = ?&quot;,new String[]{&quot;1&quot;});

I am not sure but I think that the problem may be setting Price column type as text. If so, change it integer.

答案3

得分: 0

要更新您的 SQL 数据库,请使用以下代码:db.update(TABLE_NAME, values, where, whereArgs); 您可以在此处阅读相关信息。用例可能如下所示:

ContentValues cv = new ContentValues();
cv.put("column_name", "updated value");

db.update("table_name", cv, "column_id" + "=?", new String[]{"primary_id"});
英文:

To update your sql db use db.update(TABLE_NAME, values, where, whereArgs); You can read about here.The use case can look like this:

ContentValues cv = new ContentValues();
cv.put(&quot;column_name&quot;, &quot;updated value&quot;);

db.update(&quot;table_name&quot;, cv, &quot;column_id&quot; +  &quot;=?&quot;,new String[]{&quot;primary_id&quot;});

答案4

得分: 0

update() 只适用于 ContentValues,它是用于 SQL 变量绑定的包装器。您只能绑定值,而不能绑定诸如 Price + 50 之类的表达式。

要使用类似的表达式,可以使用带有您已经拥有的原始 SQL 字符串的 execSQL()

英文:

update() only works with ContentValues which is a wrapper for SQL variable binding. You can only bind values, not expressions like Price + 50.

To use an expression like that, use execSQL() with the raw SQL string you already have.

答案5

得分: 0

你也可以尝试

`db.execSQL(
  "UPDATE Products SET Price = ? WHERE ProductID = ?",
    new String[] {Price+50 , "1"}
);`
英文:

You can also try

db.execSQL(
&quot;UPDATE Products SET Price = ? WHERE ProductID = ?&quot;,
new String[] {Price+50 , &quot;1&quot;}
);

huangapple
  • 本文由 发表于 2020年7月27日 16:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63111688.html
匿名

发表评论

匿名网友

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

确定