Query Local Database with targeted callback and custom arguements


/ Published in: JavaScript
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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. }

URL: http://www.digitalsurgeons.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.