First I know WebKit do not allow Make synchronous requests to SQLite database. And I'm start playing with it and tried to assign result to global variable "data". I still not sure if it's possible but want to ask you.
I'm created global object to store respond from DB
var data = {};Here is DB class
var db = {
mydb: false,
init: function () {
try {
if (!window.openDatabase) {
alert('not supported');
} else {
this.mydb = openDatabase('test_db', '1.0', 'Test DB', 1024*1024*5);
}
} catch(e) {
// Error handling code goes here.
if (e == INVALID_STATE_ERR) {
// Version number mismatch.
alert('Invalid database version.');
} else {
alert('Unknown error '+e+'.');
}
return;
}
},
exec: function (query, params) {
try {
this.mydb.transaction(function(transaction) {
transaction.executeSql(query, params, db.dataHandler, db.errorHandler);
});
} catch(e) {
alert(e.message);
}
},
dataHandler: function (transaction, results) {
// Handle the results
data = results.rows;
return true;
},
errorHandler: function (transaction, error) {
// returns true to rollback the transaction
alert('Code: '+error.code+'\nMessage: '+error.message);
return true;
},
}
- And finally here is the problem:
$(function () {
db.init();
db.exec('SELECT * FROM errors;');
alert(Log.object_toString(data) ); // this alert show empty object as I declared in first line
alert(Log.object_toString(data) ); // this one return object with responded data from database
});
So the problem is I can't manipulate with "data" right after db.exec() call, but if I make alert() after transaction then data will populate with all information.
Any ideas how can I avoid it?