Update values of a specific column of a specific row. Android SQLite -
i have recyclerview cardholder inflates using values of 'name' table 'ordertable'. cardholder have edittext displays values of column 'quantity' in sqlite database.
i have button update database every changes in edittext.
i have table ordertable
id name quantity 1 order1 1 2 order2 1 3 order3 1 4 order4 1
being more specific, how can update quantity of order2 on onbuttonpressed new values of edittext.
edit...
i have tried code nothing happened
button update values
public void addbuttonclick(textview tv_cardrowname_atc, textview tv_currentprice_atc, edittext et_quantity_atc, int position) { int thisquantity = integer.parseint(et_quantity_atc.gettext().tostring()); thisquantity++; string ordername = tv_cardrowname_atc.gettext().tostring(); string oldquantity = et_quantity_atc.gettext().tostring(); string newquantity = string.valueof(thisquantity); sqlitecblcadapter.selectupdateitem(ordername, oldquantity, newquantity); et_quantity_atc.settext(string.valueof(thisquantity)); }
update method
public string selectupdateitem(string ordername, string oldquantity, string newquantity) { sqlitedatabase sqlitedatabase = sqlitecblc.getwritabledatabase(); string[] columns = {sqlitecblc.col_ordname, sqlitecblc.col_quantity}; cursor cursor = sqlitedatabase.query(sqlitecblc.table_name, columns, sqlitecblc.col_ordname + " = '" + ordername + "'", null, null, null, null); stringbuffer stringbuffer = new stringbuffer(); while (cursor.movetonext()) { int index1 = cursor.getcolumnindex(sqlitecblc.col_ordname); int index2 = cursor.getcolumnindex(sqlitecblc.col_quantity); string order = cursor.getstring(index1); string quantity = cursor.getstring(index2); contentvalues contentvalues = new contentvalues(); contentvalues.put(sqlitecblc.col_quantity, newquantity); string[] whereargs = {quantity}; sqlitedatabase.update(sqlitecblc.table_name, contentvalues, sqlitecblc.col_quantity + " =? ", whereargs); stringbuffer.append(order); } return stringbuffer.tostring(); }
the easiest way achieve use sql update query follows:
from sqlite web site:
the sqlite update query used modify existing records in table. can use clause update query update selected rows, otherwise rows updated.
the syntax update query follows:
update table_name set column1 = value1, column2 = value2...., columnn = valuen [condition];
so in case sql update query thing this:
update ordertable set quantity = (insert value of edit text) name = 'order2'
you can execute query using execsql() method of sqlitedatabase object have , passing in sql query above string parameter.
Comments
Post a Comment