android - updating or inserting in a table with keeping the row numbers to one -
i trying see if there in table update , if not insert 1 row. have 1 row in table.
public void createorupdate(long id,string x, string y, string angle){ string[] columns= new string[]{key_rowid, key_x, key_y, key_angle}; cursor c= ourdatabase.query(database_table, columns, null, null, null, null, null); contentvalues editcon = new contentvalues(); if (c!=null){ editcon.put(key_x, x); editcon.put(key_y, y); editcon.put(key_angle, angle); ourdatabase.update(database_table, editcon, key_rowid + "=?", new string[]{string.valueof(id)}); } else{ editcon.put(key_x, x); editcon.put(key_y, y); editcon.put(key_angle, angle); ourdatabase.insert(database_table, null, editcon); } } public string getdata() { // todo auto-generated method stub string[] columns= new string[]{key_rowid, key_x, key_y, key_angle}; cursor c= ourdatabase.query(database_table, columns, null, null, null, null, null); string result= ""; int irow= c.getcolumnindex(key_rowid); int ix= c.getcolumnindex(key_x); int iy= c.getcolumnindex(key_y); int iangle= c.getcolumnindex(key_angle); int i= 0; for( c.movetofirst(); !c.isafterlast(); c.movetonext()){ result= result+ c.getstring(irow)+ " "+ c.getstring(ix)+ " " +c.getstring(iy)+ " "+ c.getstring(iangle)+"\n"; } return result; }
but when use entry.createorupdate(1, double.tostring(db1), double.tostring(db2), double.tostring(db3));
in code there nothing in table , no exception , nothing. when use update or insert separately works.. problem?
the check c!=null
not enough. cursor can not null when result of query empty. means need amount of rows using c.getcount()
, check if returns 0
or 1
.
ps: for( c.movetofirst(); !c.isafterlast(); c.movetonext()){
pretty ugly use loop. should change while(c.movetonext())
.
movetonext()
same movetofirst()
on fresh created cursor object.
Comments
Post a Comment