PhoneGap iPhone中的SQLite

时间:2011-04-04 09:20:32

标签: javascript iphone sqlite html5 cordova

我正在尝试在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循环执行后发生,并且它只被调用一次,这使得只传递的最终值被添加到表中。 (我只收到一次警告“enterIntoTransaction”,并且该值已添加到数据库中。非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我通过放置

解决了这个问题

transaction.executeSql()

在for循环中

并迭代它。