Update row in SQlite database by row position in android -


i have database contains "date" column , "item" column. want user update specific row in database. trying update method in sqlitedatabase class. problem dont know how make update method find row want. saw example use parameters 1 word. this:

ourdatabase.update(tablename, cvupdate, rowid + "=" + item , null);  

my problem want update row have specific item , date. name of item alone not enough. tried code below didnt work, hope youll can me.

public void updateentry(string item, string date) throws sqlexception{  string[] columns = new string[]{myitem, mydate}; cursor c = ourdatabase.query(tablename, columns, null, null, null, null, null);          long position;          contentvalues cvupdate = new contentvalues();         cvupdate.put(date, mydate);         cvupdate.put(item, myexercise);           int itemall = c.getcolumnindex(myitem);         int dateall = c.getcolumnindex(mydate);          (c.movetofirst(); !c.isafterlast(); c.movetonext()){             if (c.getstring(itemall).equals(myitem) && c.getstring(dateall).equals(mydate))             {                 position = c.getposition();                 break;             }         }          ourdatabase.update(tablename, cvupdate, rowid + "=" + position , null);      } 

first, columns string[] supposed contain column names, such "_id", or whatever column names have used. given compare content of column myitem object myitem, assume there confusion somewhere here.

secondly, rowid , position different things in sql, if delete rows, row id autoincrement, , since query not explicitely sorted. replacing c.getposition() c.getlong(c.getcolumnindex(id_column)) make more sense.

thirdly, sql nice because can query it. example, rather items , loop find matching date , item, can :

string whereclause = item_column + " = ? , " + date_column + " = ?"; string[] whereargs = new string[] { item, date }; cursor c = ourdatabase.query(tablename, columns, whereclause, whereargs, null, null, null); 

instead of loop.

forthly, can make query in update :

string whereclause = item_column + " = ? , " + date_column + " = ?"; string[] whereargs = new string[] { item, date }; ourdatabase.update(tablename, cvupdate, whereclause, whereargs);  

extra tip: use full caps variable names contants such column names, readability.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -