Neerav Shah
Neerav Shah

Reputation: 743

How to execute tx.executeSql function inside another Tx.executeSql function synchronously in phone gap android

I am creating an app in which I want to fetch data from the database local. I am having 3 tables. what I want is when I fetch a data from first table say from employe details I check few condition if employee is superuser then it will fetch data from second table project details. So what I want is when the employee is Superuser it should start to execute the data fetch from second table and then move further. But what happening is when it get the superuser condition and as soon as it found tx.executeSql function inside it makes the thread and continue the further execution from the first table and after completing it start executing data from project details. Can any one help to make this execution work in flow that I want.

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 ;
                }
            }
        });
    });

In the above code when a user type is su it then instead of executing the second second function as soon as it detects the tx.executeSql function it continue the first query execution and then runs the tx.execute in the SU condition what I want is if user type is Su it should then complete the tx.execute function inside it and then continue the further execution because there data is need in the last.

Upvotes: 2

Views: 7120

Answers (1)

CL.
CL.

Reputation: 180060

The executeSql callback is always executed asynchronously.

You should write your code so that each "some execution here" code is independent of any other code.

Alternatively, you could get all the data you want with joins so that you have a single result set:

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 {
            ...
        }
    }
});

Note: if there is more than one matching accessRights/usergroups record for one users record, you will get multiple records with duplicated users data in the result.

Upvotes: 2

Related Questions