I've made a function myFunction which performs simple task i.e. to retrieve data from sqlite database and save it in a array which can be used easily. This function should follow these steps to fulfill my functionality.
- To retrieve data from database.
- To save it into array.
- return array
But it is performing these steps in this sequences; 3, 1, 2, so I am not getting the data because it returns without getting it.
e.g.
function myFunction() {
var ret;
var arr = [];
db.transaction(function (trans) {
trans.executeSql('SELECT * FROM tblname', [],
function (transaction, result) {
if (result != null && result.rows != null) {
for (var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
ret = row.urlcolmn;
arr.push(ret);
alert('alert 1'); // just to check which line(alert) is called first
}
}
}, errorHandler);
}, errorHandler, nullHandler);
alert('alert 2'); // just to check which line(alert) is called first
return arr;
}
In above code alert 2 is shown before alert 1..... that's why it doesn't return value in array. This problem can be because sqlite in JS is Asynchronous.