java - Performing rawQuery and place resulting value for each row in database -
so on add database, perform select statement result , add separate column in database, , looks follows:
public void addgaslog(gaslog gaslog){ // reference writable db sqlitedatabase db = this.getwritabledatabase(); // create contentvalues add key "column"/value contentvalues values = new contentvalues(); //... cursor cursor = getwritabledatabase().rawquery("select (" + gaslog.getodometer() + " - y.odometer) / " + gaslog.getgallons() + " mpg gaslog x, gaslog y y.odometer = (select max(z.odometer) gaslog z z.odometer < " + gaslog.getodometer() + ")", null); try { if (cursor.movetofirst()) { mpg = cursor.getstring(0); } } { cursor.close(); } values.put(key_mpg, mpg); log.d("mysqlitehelper", key_mpg); // insert long gaslog_id = db.insert(table_gaslog, //table null, //nullcolumnhack values); // key/value -> keys = column names/ values = db.close(); } so good, performs calculation needed in select statement , inserts correct value in mpg column row.
what need able use similar functionality, , perform on each row in database... run select statement on each row, , pass value mpg column each row hits.
i know few values change, gaslog.getodometer() need instead value of "odometer" column row , gaslog.getgallons() need value "gallons" column row.
any appreciated.
edit: looking put results question new method can run separately, go through each row on database , update them all.
edit2:
this method created creating blank columns
public void updatempg(){ sqlitedatabase db = this.getwritabledatabase(); cursor cursor = getwritabledatabase().rawquery("select ('odometer' - y.odometer) / 'gallons' mpg gaslog x, gaslog y y.odometer = (select max(z.odometer) gaslog z z.odometer < 'odometer')", null); contentvalues values = new contentvalues(); try { cursor.movetofirst(); while (true) { mpg = cursor.getstring(0); values.put(key_mpg, mpg); long gaslog_id = db.insert(table_gaslog, null, values); if (cursor.islast()) break; cursor.movetonext(); } } { cursor.close(); } db.close(); } edit3: 
here how go through rows in database , modify mpg column value in table_gaslog table.
cursor cursor = getwritabledatabase().rawquery("select (" + gaslog.getodometer() + " - y.odometer) / " + gaslog.getgallons() + " mpg gaslog x, gaslog y y.odometer = (select max(z.odometer) gaslog z z.odometer < " + gaslog.getodometer() + ")", null); try { cursor.movetofirst(); int key = 1; while (true) { mpg = cursor.getstring(0); values.put(key_mpg, mpg); db.update(table_gaslog, values, "_id=?", new string[]{string.valueof(key)}); if (cursor.islast()) break; cursor.movetonext(); key++; } } { cursor.close(); } db.close(); this code iterate on entire result set , extract mpg value in each row , insert table_gaslog database.
edit: add more rows table_gaslog. comment, understand need update row. in case, instead of db.insert() method, please use:
update(string table, contentvalues values, string whereclause, string[] whereargs) your clause depend on whatever condition is.
Comments
Post a Comment