i have problem with a "select" query. In my case, i have a loop (for the first) that scan my Array and for the current value my query must give a result. The code is this below:
for (var key in arrayPlace) {
place = arrayPlace[key].replace(/'/g, '"');
place=$.trim(place);
//Id place
sqls.executeSql("SELECT * FROM luogo_accomodation WHERE nome_luogo LIKE '"+place+"'",[], function(tx,result){
//alert(sqls);
if(result.rows.length==0){
alert("Error.");
}else{
//alert("Found!");
var len = result.rows.length, i;
for (i = 0; i < len; i++){
idLuogo=result.rows.item(i).id_luogo;
}
}
}, null);
//alert(idLuogo);
}
If i print the value of a variables "key" or "place" in the for (the first) i will have as a result the current value of the scan of the Array, but if i print this variables ("key" or "place") inside the query result i will have only last value of my Array.
So if i print the value of variable "idLuogo" after instruction "excetueSql", commented "alert(idLuogo)", i will have the last result of my Array executed in the query.
I can not understand what is the order of execution of the loop and the query.
Can you help me?
Thanks.
-
I am sorry, it is very hard to understand you. Especially, I have no idea what you mean by "print ... inside the query result", and also when you say "the loop" which of the two loops in your code you're referring to. Could you edit your question to make it clear what exactly you are asking?Amadan– Amadan2014年07月02日 08:27:41 +00:00Commented Jul 2, 2014 at 8:27
-
I edited my question, I hope I was a little clearer now. Excuse me.Lorenzo Belfanti– Lorenzo Belfanti2014年07月02日 08:59:13 +00:00Commented Jul 2, 2014 at 8:59
1 Answer 1
This has to do with asynchronous execution. To simplify your code to important bits:
for(var key...) {
// A
...
executeSQL(...., function(..., result) {
// B
...
}, ...);
// C
}
This says:
For each
key, first do A. Then tell the database "do some SQL, and when it is done, let me know so I can do B". Then do C, and repeat.
So, lets say you have two keys, foo and bar. The execution is as follows:
- A gets executed for
foo - Database gets told to do SQL for
foo, and to do B when it's done. - C gets executed for
foo - Loop restarts
- A gets executed for
bar - Database gets told to do SQL for
bar, and to do B when it's done. - C gets executed for
bar - Loop finishes
- The thread control returns to the runtime, which is now free to execute other things (i.e. to start listening for SQL's requests to do B). If SQL is done before this time, it will have to wait.
- Some time later,
fooSQL is done, and executes B forfoo. - Also some time later (could be even before
fooSQL is done),barSQL is done, and executes B forbar`.
Now, in your B, you first display idLuogo, then you set it to a new value. This means that when B for foo executes, it will print nothing (since idLuogo hasn't been set yet), but when B for bar executes, it will print the last value that has been set there (which is the one from B for foo).
If you print key in B, the key loop has already done, so you're getting the last value it had while the loop was executing.
4 Comments
idAccomodation again has the same problem - its value is only there in that one line, so you have to use it there. Since idAccomodation is a global variable, it will keep getting overwritten just like idLuogo was. You need to ask yourself rather, "what do I want to do with this result?"Explore related questions
See similar questions with these tags.