I am developing a phonegap app which will use a sqlite database. The only updating of this database will involve its contents and not its scheme.
The approach to keep the local sqlite database updated is:
-
There is a remote db with a table with two columns: ‘id’ and ‘sql_sentence’. It stores the set of sql_sentences to be executed locally so the local sqlite db is updated.
-
Locally I have a var ‘local_max_id’ that keeps the id of the last sql_sentence executed locally. The first time the app is run its value is 0.
-
Every time the app starts it compares the value of ‘local_max_id’ with the highest value in column ‘id’ of the remote DB.
-
If the values match then local DB is up to date. Otherwise I must retrieve the set of sql_sentences with an id bigger than ‘local_max_id’, execute it and update ‘local_max_id’ to the last received row id.
The code to execute this (there are some Spanish messages but I have tried to give necessary info in English):
var min_id;
var local_max_id=0;
var sentencia_sql = "";
function initDB(){
//Just to make sure it requests the database info. Out when in production
window.localStorage.setItem("local_max_id", 0);
// Used to see which has been the last sql_sentence id executed locally
local_max_id = window.localStorage.getItem("local_max_id");
//Call to retrieve the highest id in remote database
$.post("http://mytests.es/phonegap-db/get-db.php", {exec:"max_id"}, treat_max_id, "json");
}
//Method that decides what to do depending on returned remote_max_id value and local_max_id.
function treat_max_id(remote_max_id){
if(remote_max_id > local_max_id){
$.post("http://mytests.es/phonegap-db/get-db.php", {exec:"get_sentencias", "min_id":local_max_id},
function(res) {
if(res){
for(j=0;j<res.length;j++){
sentencia_sql = res[j].sentencia;
alert(sentencia_sql);
//The problem is here. While the alert shows the proper sql_sentence of each iteration, this variable (sentencia_sql) only has the last returned value inside transaction function, but is executed N times (being N the amount of returned sql_sentences)
transaction(
function(tx){
console.log(sentencia_sql);
tx.executeSql(sentencia_sql);
},
errorDB,
succesDB2
);
}
window.localStorage.setItem("local_max_id", remote_max_id);
}
else{
console.error("No se ha recuperado resultado de get-db.php/get_sentencias");
}
},"json"
);
}
}
The loop that treats the result with the N sql sentences to be executed behaves in a way that is difficult for me to understand. Even though the method ‘transaction’ that is supposed to execute the sql sentence locally is inside the loop and is executed once per received row, the variable ‘sentencia_sql’ always has the same value, the last received sql sentence.
Is it normal? Should I use any instructions to control this asynchronous behaviour?
Asynchronous JavaScript takes some getting used to – code doesn’t run in the order it is written, and all iterations of a loop share the same scope.
Adding rows to a sqlite database using a loop (Phonegap)