Return Websql query as Array for a global variable

Asked

Viewed 316 times

-1

I am using a Websql database to save usage license information in a Phonegap application. At any time of the system I will need to access this information, so I intend to save this into a global variable in the form of an array. This license of use, when opening the program I run a function to search the information in the Websql of the same, but I am not able to insert the return of the function within this global variable. Here comes the code:

<!-- language: lang-js -->


//license
LIC = [];

//WebSQL Connect
conn = openDatabase('test', 'test', 'test', 200 * 1024 * 1024);

//return license
function(){

    conn.transaction(function (tx) {

        //sql
        tx.executeSql('SELECT * FROM license', [], function (tx, results) {

            var len = results.rows.length, i;
            //create array response

            for (i = 0; i < len; i++){

                //insert values on response
                LIC['cpfCnpj']     = results.rows.item(i).cpfCnpj;
                LIC['serial']      = results.rows.item(i).serial;
                LIC['apiHash']     = results.rows.item(i).apiHash;
                LIC['status']      = results.rows.item(i).status;

            };

            console.log(LIC); //aqui LIC retorna

        }, null);

        console.log(LIC); //aqui não retorna

    });

}

console.log(LIC); //aqui não retorna

someone has a light there to help me?

  • does the following, in console.log(LIC); put to show with an identifier to differentiate one from the other, like console.log("1º - " + LIC);, and puts the order in which he returned the console.log and put here, because very likely console.log(LIC); of Callback be the last to be executed for being asynchronous, so the others who will run regardless of Callback’s response and will not have anything at all

1 answer

0

By the fact of the method executeSql be asynchronous, and have to use Callback, which will only be executed when everything related to it is "OK", the program ends up executing the bottom lines, and for this reason there are 2 console.log(LIC); which are empty because they are executed before the Callback be executed, and only what is within the Callback is completed.

What can be done basically is to call another function at the end of Callback to continue with what you want to be done, without having more complications.


Tip:

As in your case it is a query that can be considerably large, it is good practice to show something on the screen while making the query, some kind of progress bar.

  • in my case, I just need him to make such a query and return this value to me in a global variable. and that so I can access the variable whenever I need

  • all right, but this global variable will only be "fed" when the Callback is executed

  • in your case, the Callback is the function (tx, results).... and its LIC variable will only be fed after this Function is executed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.