android - SQLite same result after update -
strange behaviour of sqlite update in contentprovider.
update method:
@override public int update(uri uri, contentvalues updatevalues, string whereclause, string[] wherevalues) { sqlitedatabase db = taskscontentprovider.dbhelper.getwritabledatabase(); int updatedrowscount; string finalwhere; db.begintransaction(); // perform update based on incoming uri's pattern try { switch (urimatcher.match(uri)) { case matcher_tasks: updatedrowscount = db.update(taskstable.table_name, updatevalues, whereclause, wherevalues); break; case matcher_task: string id = uri.getpathsegments().get(taskstable.task_id_path_position); finalwhere = taskstable._id + " = " + id; // if passed 'where' arg, add our 'finalwhere' if (whereclause != null) { finalwhere = finalwhere + " , " + whereclause; } updatedrowscount = db.update(taskstable.table_name, updatevalues, finalwhere, wherevalues); break; default: // incoming uri pattern invalid: halt & catch fire. throw new illegalargumentexception("unknown uri " + uri); } } { db.endtransaction(); } if (updatedrowscount > 0) { dvsapplication.getcontext().getcontentresolver().notifychange(uri, null); } return updatedrowscount; }
query method:
@override public cursor query(uri uri, string[] selectedcolumns, string whereclause, string[] wherevalues, string sortorder) { sqlitequerybuilder qb = new sqlitequerybuilder(); // choose projection , adjust "where" clause based on uri pattern-matching. switch (urimatcher.match(uri)) { case matcher_tasks: qb.settables(taskstable.table_name); qb.setprojectionmap(tasksprojection); break; // asking single comic - use rage comics projection, add clause return 1 // comic case matcher_task: qb.settables(taskstable.table_name); qb.setprojectionmap(tasksprojection); // find comic id in incoming uri string taskid = uri.getpathsegments().get(taskstable.task_id_path_position); qb.appendwhere(taskstable._id + "=" + taskid); break; case matcher_task_comments: qb.settables(taskcommentstable.table_name); qb.setprojectionmap(taskcommentsprojection); break; case matcher_task_comment: qb.settables(taskcommentstable.table_name); qb.setprojectionmap(taskcommentsprojection); string commentid = uri.getpathsegments().get(taskcommentstable.task_comment_id_path_position); qb.appendwhere(taskcommentstable._id + "=" + commentid); break; default: // if uri doesn't match of known patterns, throw exception. throw new illegalargumentexception("unknown uri " + uri); } sqlitedatabase db = taskscontentprovider.dbhelper.getreadabledatabase(); // 2 nulls here 'grouping' , 'filtering group' cursor cursor = qb.query(db, selectedcolumns, whereclause, wherevalues, null, null, sortorder); // tell cursor uri watch, knows when source data changes cursor.setnotificationuri(dvsapplication.getcontext().getcontentresolver(), uri); return cursor; }
trying update , row.
int affectedrowscount = provider.update(uri.parse(taskstable.content_uri.tostring() + "/"+ taskid), task.getcontentvalues(), null, null);
affectedrowscount
eqaul 1
check if row updated
cursor cs = provider.query(taskstable.content_uri, new string[] {taskstable.task_state_value}, taskstable._id +" = ?", new string[] {string.valueof(taskid)}, null); if(cs.movetofirst()) { string state = cs.getstring(cs.getcolumnindex(taskstable.task_state_value)); }
state
same before update. though update went succesful because affectedrowscount
equal 1 selecting same id same row seems row wasn't updated @ all.
in update
method using transaction
, never set result successful, everytime reach db.endtransaction()
rollback
performed. that's why update isn't stored.
the changes rolled if transaction ended without being marked clean (by calling settransactionsuccessful). otherwise committed.
you need use
db.settransactionsuccessful();
when update finished without errors. in code, should after both db.update
.
Comments
Post a Comment