How to add a value of a variable in a sqlite database existing column?
Solution 1:
You can use a subquery in the WHERE
clause argument of the update()
method, so that there is no need for a SELECT
query to retrieve the last id
:
public static void addScore(int studentScore) {
String table = DataContract.StudentTable.TABLE_NAME1;
String id = DataContract.StudentTable.COLUMN_ID;
ContentValues cv = new ContentValues();
cv.put(DataContract.StudentTable.COLUMN_SCORE, studentScore);
db.update(
table,
cv,
id + " = (SELECT MAX(" + id + ") FROM " + table + ")",
null
);
db.close();
}