Java 如何将数据插入到id=…的现有列中。。。?

Java 如何将数据插入到id=…的现有列中。。。?,java,android,sqlite,android-sqlite,Java,Android,Sqlite,Android Sqlite,我在数据库中创建了一个包含分数(10条记录)的表。他们现在是空的,但我希望他们被更新后,用户做了一些测试 现在我的函数如下所示: public boolean insertScore(float score, int id){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues

我在数据库中创建了一个包含分数(10条记录)的表。他们现在是空的,但我希望他们被更新后,用户做了一些测试

现在我的函数如下所示:

public boolean insertScore(float score, int id){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("Score", score);
        long result = db.insert("Scores" , null, contentValues);
        if (result == -1){
            return false;
        }
        else{
            return true;
        }
    }


但我想把数据放在id等于id参数的行中。如何操作?

您需要的是更新列
score
的现有值,而不是插入新行:

public boolean updateScore(float score, int id){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("Score", score);
    long result = db.update("Scores", contentValues, "id = ?", new String[] {String.valueOf(id)});
    db.close();
    return result > 0;
}

方法
update()
返回受影响的行数,因此如果更新了具有指定
id
的行,则方法
updateScore()
将返回
true

有效。谢谢你:D!