Getting a webkit executeSql transaction to return a value

How can I get the following JavaScript to return row so that I can access it outside of the transaction? All of Apple's example code seems to be written in HTML in a browser within a transaction, and not data is ever passed back to the calling function.

Line by line:

 function getData() { db.transaction(function(tx) { tx.executeSql("SELECT id FROM table LIMIT 1", [], function(tx, result) { row = result.rows.item(0); }, function(tx, error) { }); }); return row; } 

Is it possible? Can Webkit Storage API be installed synchronously instead of asynchronous execution?

+6
javascript sqlite webkit
source share
3 answers

I think you want to create a closure here, as garbage values ​​are collected / removed from the scope chain before you can access them. Pass row to the close to access later, or some other function that can handle the value while it is still in scope.

Additional information: Work with closing

+5
source share

I understand this is a very old question, but I found it when I was looking for how to handle asynchronous SQLite calls in JavaScript. And the question is the same as mine, and I found the best answer (unfolds according to the selected answer, using closure)

my version of your getData function is as follows:

 function get_option (option, get_option_callback){ if (db === null){ open_database(); } db.transaction(function (tx) { tx.executeSql("SELECT rowid,* FROM app_settings WHERE option = ? ", [option], function(tx, result){ item = result.rows.item(0); get_option_callback(item.value); return; } }, sql_err); }); } 

Then, to call the method, I would use:

 get_option("option name", function(val){ // set the html element value here with val // or do whatever $('input[name="some_input"]').val(val); }); 
+5
source share

I wrote an example of this and other SQL transactions: http://wecreategames.com/blog/?p=219

You must execute executeKql WebKit calls in an asynchronous style. To get around this, you must have:

 function(tx, error) { } 

do something to update your data. Something like:

 function(tx, results) { console.log("Results returned: "+results.rows.length); for (var i=0; i<results.rows.length; i++) { var row = results.rows.item(i); document.getElementById('latestUpdated').innerHTML = row; } } 

Please note that the second variable in the function is not an error, these are the results.

I set up a for loop to show that several results can be returned (maybe not with this SQL statement) - so hopefully you will see its usefulness.

0
source share

All Articles