Qqlite + HTML5 put results into variables

Asked

Viewed 46 times

0

And I’m doing a query in a Sqlite database and wanted the result to be stored in a javascript variable so I can work with these results in the rest of the code.

I’m doing like this:

db.transaction(function(tx) {
var ide = 1;

tx.executeSql('SELECT * FROM tab_users', [ide], function (tx, results) {
var len = results.rows.length, i;

     for (i = 0; i < len; i++) {
       var_name = results.rows.item(i).name;
       var_country = results.rows.item(i).country;
     }

});
});
alert ("Name is " + var_name + " and country is " + var_country);

But this code does not work.. only if I put the alert within the loop that way:

for (i = 0; i < len; i++) {
   var_name = results.rows.item(i).name;
   var_country = results.rows.item(i).country;

   alert ("Name is " + var_name + " and country is " + var_country);

}

I need these variables to use at the end of my html code Does anyone have any suggestions on how I’m going to do this?

  • If you put Alert OUT of the for(), it will only display the last value returned from the bank.

  • In the case of this query only displays a result even, but if I put off the db.transaction(function(tx) { doesn’t show anything at all

  • What do you want to do? Want to work with ALL the records that come back?

  • Only one record will come back... I want to work only with him

  • Try thus Results.rows.item(0). name;

  • Gave the same error.. on the console appeared: Uncaught ReferenceError: var_name is not defined

  • Now I understand, CREATE the var_name and var_country variables Out of FOR

  • I did as you said. (I actually had already done) I tried to create them out of for.. but it also didn’t work out.... :(

  • Maybe @Marcelo Bonifazio can help you.

Show 4 more comments
No answers

Browser other questions tagged

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