java - 正确使用带有 'whereClause' 参数的 db.update

标签 java android sqlite sql-update

正如我发现的那样,进行原始查询通常效果不佳,因此不建议这样做:

db.execSQL("update userData set " + hero_whose_score_to_update + "="
+ second_player_score + " where name=" + "'" + getUsername2() + "'");

相反,我尝试使用 db.update

ContentValues cv = new ContentValues();
cv.put(hero2_whose_score_to_update, second_player_score);
db.update("userData", cv, "where name = ", null);

我不知 Prop 体如何写,但我想要这个:

hero_whose_score_to_update 是一个字符串 - “hero1_score”、...“hero6_score”之一;

second_player_score 是 int,它将是某个数字。

但是 update() 中的下两个参数是什么? whereClause 和 whereArgs。 我已经阅读了 update() 的 api,但仍然不清楚,一个例子会更好。

我怀疑我应该在 whereClause 中输入一个用户名,但它应该是什么样子?

db.update("userData", cv, "where name = 'john'", null);

cv.put("hero3_score", 34) ;
db.update("userData", cv, "where name= ?", 'jonn') ;

最佳答案

whereClause 是原始 SQL 语句中 WHERE 之后的内容,但没有关键字 WHERE。

whereArgs 是一个字符串数组,因此您必须创建这样一个数组:

db.update("userData", cv, "name = ?", new String[]{ "jonn" });

关于java - 正确使用带有 'whereClause' 参数的 db.update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32604424/

相关文章:

java - Picture.writeToStream() 没有写出所有位图

java - LinearLayout 中屏幕末尾的 TextView

ios - 删除 sqlite 数据库中的所有行不起作用 - iOS

python - 使用 Python 3 和 SQLite 的批量插入性能较差

java - collections.sort 方法仅用于 List 类型的集合吗?

java - 如何从多模块项目中启动 :run on root pom. xml

java - 如何解决无法启动 Activity 错误?

android - 获取半透明Android导航栏的位置和可见性

Android Facebook 授权不工作(泄漏的窗口)

postgresql - 如何从 SpatialLite 合并到 PostGIS?