i have loop through select results. inside loop need perform update , insert transactions.
the problem sql transactions asynchronous , slow, therefore loop ends before sql operations executed, here how code works:
tx.executesql('select ... ... ...', [], function(tx, results){ (var i=0; i<results.rows.length; i++){ var id = results.rows.item(i).id; tx.executesql('update ... set ...=... id="'+ id +'";', [], function(tx, results){ console.log("update ok"); }, function(){ console.error("function error"); }); tx.executesql('insert ... (...) values (...,"'+ id +'",...)', [], function(tx, results){ console.log("insert ok"); }, function(){ console.error("function error"); }); } }, function(){ console.error("function error"); }); the loop runs faster db operations , uptades , inserts end using last 'i' of iteration. tried put insert inside update callback, still got same results.
problem solved!
tx.executesql('select ... ... ...', [], function(tx, results){ (var i=0; i<results.rows.length; i++){ var id = results.rows.item(i).id; var transaction = function(valueid){ tx.executesql('update ... set ...=... id="'+ id +'";', [], function(tx, results){ console.log("update ok"); }, function(){ console.error("function error"); }); tx.executesql('insert ... (...) values (...,"'+ id +'",...)', [], function(tx, results){ console.log("insert ok"); }, function(){ console.error("function error"); }); }(id) } }, function(){ console.error("function error"); }); had wrap inside function values right.
hope helps else. o/
Comments
Post a Comment