iPhone中的sqlite
我正在尝试在iPhone的phonegap中使用SQLite,我面临以下问题,请看一下下面的javascript代码。 非常感谢,
function testAsSeperateFunctionDB(contact)
{
database.transaction(
function( transaction ){
alert("enteringIntoTransaction");
transaction.executeSql(
'INSERT INTO testTableThree (contactId, mrMrs, firstName,
lastName, jobTitle, bActive, homePhone, contactType, accountId,
workPhone, cellularPhone, workFax, contactEmail, owner,
alternateCountry, alternateAddress1, alternateAddress2,
alternateZipCode, alternateCity, alternateProvince)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
[contact.contactId, contact.mrMrs,
contact.firstName, contact.lastName,
contact.jobTitle, contact.bActive,
contact.homePhone, contact.contactType,
contact.accountId, contact.workPhone,
contact.cellularPhone, contact.workFax,
contact.contactEmail, contact.owner,
contact.alternateCountry, contact.alternateAddress1,
contact.alternateAddress2, contact.alternateZipCode,
contact.alternateCity, contact.alternateProvince],
//Callback after successful transaction
function( transaction, results ) {
alert("added");
},
//Callback after error in transaction
function( transaction, error ) {
alert("Error" + error.code);
}
);
}
);
}
我正在使用上面的函数将一组联系人详细信息添加到表中,我将向此函数传递一个 JSON 对象。我在以下循环结构中调用上述函数。
for(i = 0; i < contactFullObjArray.length; i++) {
//Parse the ContactFull object
var contact=ContactFull.parse(contactFullObjArray[i]);
testAsSeperateFunctionDB(contact);
}
这里发生的情况是,事务仅在整个 for 循环执行后才发生,并且仅被调用一次,这使得仅传递的最终值被添加到表中。 (我只收到一次警报“enteringIntoTransaction”,并且该值正在添加到数据库中。非常感谢您的帮助
I'm trying to use SQLite in phonegap for iPhone, I'm facing the following problem, Please have a look at the below javascript code.
Thanks a lot
function testAsSeperateFunctionDB(contact)
{
database.transaction(
function( transaction ){
alert("enteringIntoTransaction");
transaction.executeSql(
'INSERT INTO testTableThree (contactId, mrMrs, firstName,
lastName, jobTitle, bActive, homePhone, contactType, accountId,
workPhone, cellularPhone, workFax, contactEmail, owner,
alternateCountry, alternateAddress1, alternateAddress2,
alternateZipCode, alternateCity, alternateProvince)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
[contact.contactId, contact.mrMrs,
contact.firstName, contact.lastName,
contact.jobTitle, contact.bActive,
contact.homePhone, contact.contactType,
contact.accountId, contact.workPhone,
contact.cellularPhone, contact.workFax,
contact.contactEmail, contact.owner,
contact.alternateCountry, contact.alternateAddress1,
contact.alternateAddress2, contact.alternateZipCode,
contact.alternateCity, contact.alternateProvince],
//Callback after successful transaction
function( transaction, results ) {
alert("added");
},
//Callback after error in transaction
function( transaction, error ) {
alert("Error" + error.code);
}
);
}
);
}
I'm using the above function to add a set of contact details into a table, I will be passing a JSON object to this function. I'm calling the above function in the following loop structure.
for(i = 0; i < contactFullObjArray.length; i++) {
//Parse the ContactFull object
var contact=ContactFull.parse(contactFullObjArray[i]);
testAsSeperateFunctionDB(contact);
}
What's happening here is transaction is happening only after the whole for loop is executed, and it is getting called only once, which makes only the final values that are passed to be added into the table. (I'm getting the alert "enteringIntoTransaction" only once, and that value is getting added into the database. Thanks a lot for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过将
transaction.executeSql()
放在for 循环中并迭代它来解决这个问题。
I solved this by placing the
transaction.executeSql()
inside the for loop and iterated through it.