正确使用 db.update 和 'whereClause' 参数

Proper use of db.update with 'whereClause' argument

正如我所发现的,进行原始查询通常效果不佳,因此不推荐:

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);

我不知道具体怎么写,但我想要这个:

hero_whose_score_to_update 是一个字符串 - "hero1_score", ..."hero6_score";

之一

second_player_score是int,会是一些数字。

但是 update() 中接下来的 2 个参数是什么? 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" });