【问题标题】:How to execute tx.executeSql function inside another Tx.executeSql function synchronously in phone gap android如何在电话间隙android中同步执行另一个Tx.executeSql函数内的tx.executeSql函数
【发布时间】:2013-10-28 06:55:37
【问题描述】:

我正在创建一个应用程序,我想在其中从本地数据库中获取数据。我有 3 张桌子。我想要的是当我从第一个表中获取数据时说从员工详细信息中我检查了一些条件,如果员工是超级用户,那么它将从第二个表项目详细信息中获取数据。所以我想要的是当员工是超级用户时,它应该开始从第二个表中执行数据提取,然后进一步移动。但是发生的情况是,当它获得超级用户条件时,一旦在其中找到 tx.executeSql 函数,它就会创建线程并继续从第一个表执行进一步执行,并在完成后开始执行项目详细信息中的数据。 任何人都可以帮助我按照我的意愿进行这项工作吗?

function getusersdetails()
{
    db.transaction(function(tx){
        tx.executeSql('select * from users',[],function(tx,results){
            var dataset = results.rows.length;
            if(dataset>0)
            {
                for(var i=0;i<dataset;i++)
                {
                    if(results.rows.item(i).employ_type =='SU')
                    {
                        tx.executeSql('Select * from accessRites where user_Id = results.rows.item(i).user_id',[],function(tx,result){
                            //some execution here;
                        });
                    }
                    else if(results.rows.item(i).employ_type =='Manager')
                        {
                            tx.executeSql('Select * from usergroups where user_Id =  results.rows.item(i).user_id',[],function(tx,results){
                                // some execution here;
                            });
                        }
                    else
                    {
                        //some execution here;
                    }
                    //here some usere execution ;
                }
            }
        });
    });

在上面的代码中,当用户类型为 su 时,它不会在检测到 tx.executeSql 函数后立即执行第二个函数,而是继续执行第一个查询,然后在 SU 条件下运行 tx.execute我想要的是如果用户类型是 Su 它应该完成其中的 tx.execute 函数,然后继续进一步执行,因为最后需要数据。

【问题讨论】:

  • 伙计们,我只想问,如果条件为真,如何在另一个 tx.executeSql 中同步执行 tx.executeSql 函数,然后继续执行..
  • 在您保密的情况下,我们如何为您的代码提供帮助?
  • @CL 我已经放置了代码,请从保留中删除它......
  • 我需要再进行一次“重新开放”投票...

标签: android sqlite cordova-2.0.0


【解决方案1】:

executeSql 回调始终异步执行。

您应该编写代码,以便每个“在此处执行某些操作”的代码都独立于任何其他代码。

或者,您可以通过连接获取所需的所有数据,以便拥有一个结果集:

tx.executeSql('SELECT * FROM users' +
              ' LEFT JOIN accesRites USING (user_Id)' +
              ' LEFT JOIN usergroups USING (user_Id)',
              [], function(tx, result){
    for (var i = 0; i < results.rows.length; i++) {
        if (results.rows.item(i).employ_type =='SU') {
            ...
        } else if (results.rows.item(i).employ_type =='Manager') {
            ...
        } else {
            ...
        }
    }
});

注意:如果一条users记录有多个匹配的accessRights/usergroups记录,则结果中会出现多条users数据重复的记录。

【讨论】:

  • 我已经以相同的方式返回代码,但是如果我想在条件为真时从另一个表访问数据,然后在访问数据后它应该继续执行。
  • 异步回调无法继续。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多