0

I am trying to iterate an dynamic array as input for select query. The select query is not firing for all array elements. I wrote select query in for loop, the problem is loop is firing first and the select query is performing only for last element of array. may be this due to asynchronous function. How can I resolve this, My code is as follows,

function sendCategoryDetailsNew(myLocation)
{
     var myLocationcoordinates = new Array();
     var db = window.sqlitePlugin.openDatabase({name: "MYDB"});
    for (var i = 0; i < myLocation.length; i++)
    {
    var locationName = myLocation[i];
    alert(locationName);
    db.transaction(function (tx) {
   tx.executeSql("select Coordinates from Locationlog WHERE Location = '"+locationName+"';", [], function (tx, res)
  {
   for (var i = 0; i < res.rows.length; i++)
   {
      alert("Location : "+locationName+ " Latlongs :"+ res.rows.item(i).Coordinates);
      myLocationcoordinates[i] = res.rows.item(i).Coordinates;
    }
  });
 });
 }
}

Any suggestions,

1 Answer 1

1

Try simplifying the code. I think the issue is having a for inside of a for with the same iterator name.

Here is an example of how I would try this:

function sendCategoryDetailsNew(myLocation)
{
    var myLocationcoordinates = new Array();
    var db = window.sqlitePlugin.openDatabase({name: "MYDB"});
    var locationsList = "\'" + myLocation.join("\',\'") + "\'";
    db.transaction(function (tx) {
        tx.executeSql("SELECT Location, Coordinates FROM Locationlog WHERE Location IN (?)", locationsList, function(tx, res) {
            for (var i = 0; i < res.rows.length; i++)
            {
                console.log("Location : " + res.rows.item(i).Location + " Latlongs : " + res.rows.item(i).Coordinates);
                myLocationcoordinates[i] = res.rows.item(i).Coordinates;
            }
        });
    }
}

Instead of looping through two sets of values, I have compiled the myLocation array into a string that can be used in an IN sql statement. This will return all results for the myLocation array and then iterate through them for processing.

This cuts down a lot of processing and also creates less fail points in the function.

Sign up to request clarification or add additional context in comments.

4 Comments

Will try and let you know. :)
I did not get any alert, Did I need to change the query? how will string will iterate ?
Updated answer. Shouldn't be alerting assuming there will be more than one result, changed to console.log. Also, I am assuming that since you were iterating before, that there is more than one location being passed in, this code will create a string for use in an IN statement which is used for looking up records based on a list of information.
it doesn't print in console also. Is there any way to iterate the select query over different inputs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.