Skip to content Skip to sidebar Skip to footer

Issue With Loop The Select Query In Sqlite?

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 f

Solution 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:

functionsendCategoryDetailsNew(myLocation)
{
    var myLocationcoordinates = newArray();
    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.

Post a Comment for "Issue With Loop The Select Query In Sqlite?"