Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send parameters to sqlite function on Phonegap

I have a list with some items, & a loop in which I check whether each item in the list is found in the database or not (To insert it if it's not found).

function AddItems(tx)
    {
        for(var i = 0 ; i<itemscounter; i++)
        { 
            itemI=itemsList[i]; 
            tx.executeSql('SELECT * FROM Items Where ItemsTable.ItemId="'+itemI+'"', [], AddItemsToDB, errorCB);
        }
    }

The problem is that the function waits till it finishes all the iterations then execute the sqlite statement so the itemI value is always equal to itemsList[itemscounter]. So it enters the AddItemsToDB "items counter" times, but always with the same value (the last one in the array).

This is the success Function:

function AddItemsToDB(tx, results)
{
        var len = results.rows.length;
        alert(itemI); // Always has the same value!
        if(len==0)
        {
            tx.executeSql('INSERT INTO ItemsTable (ItemId) VALUES ('+itemI +')');


        }
}

So, I'm wondering if there is some method to pass parameters at the success function itself, or if there is another method to pass the items one by one ?

like image 693
Sana Joseph Avatar asked May 26 '26 02:05

Sana Joseph


1 Answers

Yes , there is.Try something like this:

tx.executeSql('SELECT * FROM Items Where ItemsTable.ItemId="'+itemI+'"', [], 
       (function(itemI){
           return function(tx,results){
               AddItemsToDB(tx,results,itemI);
           };
       })(itemI), errorCB);

And modify AddItemsToDB like a:

function AddItemsToDB(tx, results, itemI) {
like image 103
Engineer Avatar answered May 27 '26 16:05

Engineer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!