java - How can I add to the current value in SQLite column? -
im trying add on current number in credits column of person rowid of 0. if current value 5 , user selects number 2 in spinner , clicks button, value change 7. value gets updated 2. need add/change code achieve this?
public void updateuser(int money) { string selectquery = "select " + key_credits + " " + database_table + " " + key_rowid + " = " + 0; cursor c = ourdatabase.rawquery(selectquery, null); int oldmoney = 0; if (c.movetofirst()) { oldmoney = oldmoney + integer.parseint(c.getstring(4)); } contentvalues cvupdate = new contentvalues(); cvupdate.put(key_credits, (oldmoney + money)); ourdatabase.update(database_table, cvupdate, null, null); }
you missed clause in update part
ourdatabase.update(database_table, cvupdate, key_rowid + "=?" ,new string[] { "1" });
i guess updated 2, because without clause update all entries. not updated 7 because, select statement has no results (don't know why, more code useful). check if select selects row:
public void updateuser(int money) { string selectquery = "select " + key_credits + " " + database_table + " " + key_rowid + " = " + 1; cursor c = ourdatabase.rawquery(selectquery, null); int oldmoney = 0; log.d("com.example.yourapp", c.getcount() + " rows selected"); if (c.movetofirst()) { oldmoney = c.getint(c.getcolumnindex(key_credits)); } contentvalues cvupdate = new contentvalues(); cvupdate.put(key_credits, (oldmoney + money)); ourdatabase.update(database_table, cvupdate, key_rowid + "=?" ,new string[] { "1" }); }
check log how big result is.
i hope helps you.
@edit: documentation of update @edit2: updated solution
Comments
Post a Comment