vendredi 15 janvier 2016

sqlite return statement issue

In my code, I always find that the sqlite function behaves in a random manner. This is my code

function crop_entry_done() {

    var acres=document.getElementById('acres').value;
    var cent=document.getElementById('cent').value;
    var prev_acres=find_total_cents_from_db();

    alert("prev_acres "+prev_acres);
    //var prev_cents=find_total_cents_from_db();

    var total_cents=((parseInt(acres)*100)+(parseInt(cent)));
    alert("total cents "+total_cents);
  }

function find_total_cents_from_db()
{
        var total_cents_from_db=0;
   myDB.transaction(function(tx) {
   tx.executeSql('SELECT * FROM survey_cultivation_details WHERE
survey_cultivation_id=?', [1],
   function(tx, results)
   {
          alert(results.rows.length);
          for (var i = 0; i < results.rows.length; ++i) {
                                  row = results.rows.item(i);
                                  total_cents_from_db=((parseInt(row['survey_area_acre'])*100)+(parseInt(row['survey_area_cent'])));
                                  alert("total cents from db "+total_cents_from_db);

          }
           return total_cents_from_db;
   });

   });



}

As output, I always get prev_acres as undefined and then the function goes to execute the function and gives the correct o/p value.Due to this, I am not able to identify how the function works. No matter where I give the return function I get an undefined value first. Please help!

Aucun commentaire:

Enregistrer un commentaire