I'm trying to query a WebSQL database using Javascript to return the value of the 'id' column for the single highest data line.
db.transaction(function(tx) {
//
// PROBLEM AREA
// How to get the data into a variable?
//
var xyz = 0;
tx.executeSql('SELECT MAX(id) FROM WORKOUTS ORDER BY id DESC' [],function(tx, results) {
var xyz = results.row.id, i;
alert('Alert 2');
alert(xyz);
});
}, function(){
alert('success SELECTING!');
}); // End transaction
I believe the query is executing as I am getting the success SELECTING alert.
The alert(xyz) is simply returning value nil.
How can I get the data from the SQL database into a Javascript variable?
Thanks!
EDIT: The following suggested code change results in a ReferenceError: Can't find variable: results
function myTransaction(cb)
{
db.transaction(function(tx) {
tx.executeSql('SELECT MAX(id) FROM WORKOUTS ORDER BY id DESC', [], function(tx, results) {
cb(results); });
},function(){
alert('success SELECTING!');
}); // End transaction
}
myTransaction(function(results) { alert(results); });
function myTransction(cb) { db.transaction(function(tx) { var xyz = 0; tx.executeSql('SELECT MAX(id) FROM WORKOUTS ORDER BY id DESC' [], function(tx, results) { cb(results); }); }) } myTransction(function(results) { console.log(results); });