java - Update multiple rows in Android sqlite -



java - Update multiple rows in Android sqlite -

i have table contains columns id, name, profession, age, hobby, country, sex. want update fields sex female , age 30. fields text (string). first, counting rows running loop update rows. loop running per total rows rows not updated... why? have done mistake? here code:

methods android sqlite database query:

public void updateuser(string newprofession, string newcountry, string sx, string ag) { sqlitedatabase db = this.getwritabledatabase(); string query = "update "+table_user+" set "+key_profession+"='"+newprofession+"', "+key_country+"='"+newcountry+"' "+key_sex+"='"+sx+"' , "+key_age+"='"+ag+"'"; cursor cursor = db.rawquery(query, null); cursor.close(); db.close(); } public int countall() { string countquery = "select * " + table_user; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); int cnt = cursor.getcount(); cursor.close(); db.close(); homecoming cnt; }

calling methods

public void updateusersclicked(view view) { int alldata = db.countall(); (int = 0; < alldata; i++) { db.updateuser("senior engineer", "canada", "female", "30"); system.out.println("t h s s t h e r e s u l t: " + i); } }

use execsql() , not rawquery() updates.

rawquery() compiles sql , requires 1 of moveto...() methods on returned cursor execute it. execsql() both compiles , runs sql.

also consider using ? parameters bind args in sql avoid escaping special characters , beingness vulnerable sql injection.

java android sqlite

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -