iPhone中的sqlite

发布于 2024-10-29 02:55:06 字数 1890 浏览 1 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2024-11-05 02:55:06

我通过将

transaction.executeSql()

放在for 循环中并迭代它来解决这个问题。

I solved this by placing the

transaction.executeSql()

inside the for loop and iterated through it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文