I created table in my database with scores (10 records). They are now empty but I want them to be updated after user make some quiz.
Now my function looks like this :
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;
}
}
but I would like to put the data to row with id equals the id argument. How can I do it ?
What you want is to update the existing values of the column score
and not insert new rows:
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;
}
The method update()
returns the number of the rows affected, so your method updateScore()
will return true
if the row with specified id
was updated.