将参数发送到Phonegap上的sqlite函数 [英] 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);
}
}
问题是函数等待,直到完成所有迭代然后执行sqlite语句,所以 itemI 值总是等于itemsList [itemscounter]。
所以它进入AddItemsToDB项目计数器次,但总是具有相同的值(数组中的最后一个)。
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).
这是成功函数:
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 ?
推荐答案
是,有。尝试这样:
tx.executeSql('SELECT * FROM Items Where ItemsTable.ItemId="'+itemI+'"', [],
(function(itemI){
return function(tx,results){
AddItemsToDB(tx,results,itemI);
};
})(itemI), errorCB);
并修改 AddItemsToDB
like a:
And modify AddItemsToDB
like a:
function AddItemsToDB(tx, results, itemI) {
这篇关于将参数发送到Phonegap上的sqlite函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!