Page 2 of 2

How to insert rows in a local db in a loop

Posted: Fri Jun 20, 2014 5:23 pm
by Kateryna Grynko

Hi Asif,

Requests to Database are always asynchronous, that is the issue.

Unfortunately, we don't fully understand what your code does, do we show you the following example of work with asynchronous calls.

Asynchronous call is executed in a function, and all the needed variables are saved in a closure (find more here: https://developer.mozilla.org/en-US/d...)
prevar values = [{name:"q", age:10},{name:"w", age:10},{name:"e", age:11},{name:"r", age:11},{name:"t", age:11},{name:"y", age:12},{name:"u", age:12}];
var db = window.openDatabase("posDB", "", "posDB", 1024*1000);
db.transaction( function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "menuTable"(name TEXT, age Number)');
for( var i=0; i < values&#46;length; i++) {
tx&#46;executeSql('INSERT INTO "menuTable" (name, age) values (?, ?)', [values&#46;name, values&#46;age]);
}
});

var ourFinalArray1 = {};
db&#46;transaction(function(tx) {
function exec(i) {
tx&#46;executeSql('SELECT * FROM "menuTable" WHERE age= ? ', , function(tx, results) {
var len = results&#46;rows&#46;length;
if (len 0) {
for (var j = 0; j < len; j++) { &#47;&#47; for each row
ourFinalArray1&#46;push(results&#46;rows&#46;item(j)&#46;name);
}
}
});
}
for (var i = 10 ; i < 15; i++){
ourFinalArray1 = [];
exec(i);
}
}, function(){}, function(){console&#46;log(ourFinalArray1)});/pre
Please see console output. Hope this example will be helpful.


How to insert rows in a local db in a loop

Posted: Sat Jun 21, 2014 5:46 am
by Asif

Hi Katya,
I have implemented your code and it is working fine, but my problem still exists.

In my case there is a piece of code which has to be executed only and only after this db call completed.
For ex:
pre
var values = [{name:"q", age:10},{name:"w", age:10},{name:"e", age:11},{name:"r", age:11},{name:"t", age:11},{name:"y", age:12},{name:"u", age:12}];
var db = window&#46;openDatabase("posDB", "", "posDB", 1024*1000);
db&#46;transaction( function (tx) {
tx&#46;executeSql('CREATE TABLE IF NOT EXISTS "menuTable"(name TEXT, age Number)');
for( var i=0; i < values&#46;length; i++) {
tx&#46;executeSql('INSERT INTO "menuTable" (name, age) values (?, ?)', [values&#46;name, values&#46;age]);
}
});
var ourFinalArray1 = {};
db&#46;transaction(function(tx) {
function exec(i) {
tx&#46;executeSql('SELECT * FROM "menuTable" WHERE age= ? ', , function(tx, results) {
var len = results&#46;rows&#46;length;
if (len 0) {
for (var j = 0; j < len; j++) { &#47;&#47; for each row
ourFinalArray1&#46;push(results&#46;rows&#46;item(j)&#46;name);
}
}
});
}
for (var i = 10 ; i < 15; i++){
ourFinalArray1 = [];
exec(i);
}
}, function(){}, function(){console&#46;log(ourFinalArray1)});

console&#46;log('after db call');

/pre

Here are the console logs for this code

pre
after db call VM40401:204
Object {10: Array[6], 11: Array[9], 12: Array[6], 13: Array[0], 14: Array[0]}
/pre

As you can see that though i have written the display statement "after db call" after the query, still its coming first in the console.

This is my problem because i have to run some code after the for loop completely finished.

That's why i wanted to run this query as synchronous..!!


How to insert rows in a local db in a loop

Posted: Mon Jun 23, 2014 8:03 am
by Kateryna Grynko

Hi Asif,

You can perform only asynchronous requests to Database. If you need to do something after all Database queries have worked, just add this code to a success callback function. This function is set by third parameter of method codetransaction/code (for now, you have the following code there: codeconsole&#46;log(ourFinalArray1)/code).