How can I get the following JavaScript to return row
so I can access it outside the transaction? All of Apple's example code seems to have HTML written to the browser within the transaction instead of ever passing data back to a calling function.
Along the lines of:
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 this even possible? Can the Webkit storage API be set to synchronous instead of asynchronous execution?
How can I get the following JavaScript to return row
so I can access it outside the transaction? All of Apple's example code seems to have HTML written to the browser within the transaction instead of ever passing data back to a calling function.
Along the lines of:
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 this even possible? Can the Webkit storage API be set to synchronous instead of asynchronous execution?
Share Improve this question edited Apr 11, 2009 at 18:10 ceejayoz asked Apr 11, 2009 at 17:53 ceejayozceejayoz 180k41 gold badges309 silver badges380 bronze badges 3- Are you using a JavaScript library that's openly available? Where's the example? – cgp Commented Apr 11, 2009 at 17:58
- It's not a library, it's built into Webkit. See developer.apple./safari/library/documentation/iPhone/… for details. – ceejayoz Commented Apr 11, 2009 at 18:03
- This question helped me clarify the executeSql method's available parameters^^ – Jasper Kennis Commented Jul 25, 2012 at 11:48
3 Answers
Reset to default 5I think you want to create a closure here as values are being garbage collected/moved away from the scope chain before you can access them. Pass row
to a closure for access later or to some other function that can handle the value while it's still in scope.
More info: Working With Closures
I realise this is a very old question but I found it when searching for how to deal with JavaScript asynchronous SQLite calls. And the question is the same as mine and I've found a better answer (Expands on the selected answer, using closures)
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);
});
I wrote an example of this and other SQL transactions at: http://wecreategames./blog/?p=219
You have to do the WebKit executeSql calls in an asynchronous style. To get around this, you should have your:
function(tx, error) {
}
execute 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;
}
}
Notice that the second variable into the function isn't an error, it's the results.
I put in a for loop to show that there could be multiple results returned (probably not with that SQL statement, though) -- so hopefully you see the utility of it.