Posted By

cmndo on 03/16/11


Tagged

database local html5


Versions (?)

Query Local Database with targeted callback and custom arguements


 / Published in: JavaScript
 

URL: http://www.digitalsurgeons.com

In a recent iphone webapp I developed, I needed to use safari's Local Database feature. The webapp needed to make a few SQL Queries while performing other actions.

The local database data transaction process makes it difficult to make queries and assign the data you really want. But hey, that's asynchronous event handling for you.

This function allows you to tightly couple an object with the query which will be available in the callback.

  1. function Query(sql_str, callback, args){
  2. yourDB.transaction(
  3. function (transaction) {
  4. transaction.executeSql(sql_str, [], function(transaction, results){
  5. callback(transaction, results, args);
  6. }, errorHandler);
  7. }
  8. );
  9. }
  10. //usage
  11. Query("SELECT whatever FROM wherever", doBidding, {"extra":"awesome sauce"});
  12.  
  13. function doBidding(obj){
  14. alert(obj.extra);
  15. }

Report this snippet  

You need to login to post a comment.