android - how to use context.getContentResolver() to execute a update with CASE WHEN statement -


having sql statement case update field based on row id, without need passing values.

"update accounts set field= case when id=(select id accounts id=0) 1 else 0 end"; 

how use context.getcontentresolver() execute it? or other way?

if go contentprovider , path approach i'll suggest use helper class:

public static class uribuilder{         public static final string fragment_raw_update = "rawupdate"; // here nonotify, conflict resolver pathes, etc.          private uri.builder uri;          public uribuilder(uri uri){             this.uri = uri.buildupon();         }          public uribuilder(string uri){             this.uri = uri.parse(uri).buildupon();         }          public uribuilder append(string path){             uri.appendpath(path);             return this;         }          public uribuilder append(long id){//points directly item             uri.appendpath(string.valueof(id));             return this;         }          public uribuilder rawupdate(){             uri.fragment(fragment_raw_update);             return this;         }          public uri build(){             return uri.build();         }          public static boolean israwupdate(uri uri) {             return fragment_raw_update.equals(uri.getfragment());         } } 

in content provider better have helper methods create uri brand new uribuilder, like:

public static uri contenturi(string path, long id){         return new uribuilder(base_uri)                           .append(path)                           .append(id)//optional                           .build(); }  public static uri contenturirawupdate(string path){         return new uribuilder(base_uri)                           .append(path)                           .rawupdate()                           .build(); } 

after have in code life me easier. create raw update uri:

contentresolver.update(yourcontentprovider.contenturirawupdate(dbcontract.table.content_uri), null, rawsql, null); 

and in contentprovider's update:

    @override     public int update(uri uri, contentvalues values, string selection, string[] selectionargs) {          if(uribuilder.israwupdate(uri)){             dbhelper.getwritabledatabase().update(...);             return;// exit         }         ... // standard logic matchers here         ... // dbhelper.getwritabledatabase().update(...);          ... // notify observers here } 

update: suggest understand risks , contentprovider not public. using approach can execute sql , in terms of security backdoor :)


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -