正如我发现的那样,进行原始查询通常效果不佳,因此不建议这样做:
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/