Question

I'm trying to query on for loop from a certain array. But it only saves the first element of the array. I looked at the console.log() it has been returning 1 of 1 on each for execution.

define(['require'], function(require) {
    "use strict";
    var Backbone = require('backbone');

    return Backbone.View.extend({
        render: function() {
            $('#content').fadeOut(200, function() {
                $(this).load('views/sync.html', function() {
                    $(this).trigger('create');
                    var preMadeLoc = [
                        ['L12', 'BALBALAN', 'KALINGA', 'Mountanious', '3rd', 54269, 12082, 0.22, 143201000],
                        ['L8', 'AGUINALDO ', 'IFUGAO ', 'Mountainous', '2nd', 53805, 18610, 0.35, 142708000],
                        ['L7', 'ALFONSO', 'LISTA', 'IFUGAO', 'Mountainous', '3rd', 34746, 28410, 0.82, 142707000],
                        ['L11', 'ASIPULO', 'IFUGAO', 'Mountainous', '5th', 18287, 14403, 0.79, 142711000],
                        ['L1', 'BANAUE', 'IFUGAO', 'Mountainous', '4th', 19120, 22365, 1.17, 142701000],
                        ['L17', 'CITY OF TABUK', 'KALINGA', 'Mountainous', 70025, 103912, 1.48, 143213000]
                        ['L9', 'HINGYON', 'IFUGAO', 'Mountainous', '5th', 6202, 9795, 1.58, 142709000],
                        ['L2', 'HUNGDUAN', '    IFUGAO', 'Mountainous', '4th', 26030, 9933, 0.38, 142702000],
                        ['L3', 'KIANGAN', ' IFUGAO', 'Mountainous', '4th', 20000, 15837, 0.79, 142703000],
                        ['L4', 'LAGAWE', '  IFUGAO ', 'Mountainous', '4th', 20891, 18077, 0.87, 142704000],
                        ['L5', 'LAMUT', '   IFUGAO', 'Mountainous', '4th', 15965, 23088, 1.45, 142705000],
                        ['L13', 'LUBUAGAN', 'KALINGA', 'Mountainous', '4th', 23420, 9369, 0.4, 143206000],
                        ['L6', 'MAYOYAO', 'IFUGAO', 'Mouzntainous', '4th', 23805, 16413, 0.69, 142706000],
                        ['L14', 'PASIL', 'KALINGA', 'Mountainous', '5th', 18900, 9626, 0.51, 143208000],
                        ['L15', 'PINUKPUK', 'KALINGA', 'Mountainous', '1st', 74356, 29596, 0.4, 143209000],
                        ['L16', 'RIZAL', 'KALINGA', 'Mountainous', '4th', 23100, 15942, 0.69, 143211000],
                        ['L18', 'TANUDAN', 'KALINGA', 'Mountainous', '4th', 30755, 8529, 0.28, 143214000],
                        ['L19', 'TINGLAYAN', 'KALINGA', 'Mountainous', '4th', 28300, 12557, 0.44, 143215000],
                        ['L10', 'TINOC', 'IFUGAO', 'Mountainous', '4th', 23970, 14147, 0.59, 142710000]
                    ];

                    var db = window.sqlitePlugin.openDatabase("weather-app-proper", "1.0", 'Demo', 65536);

                    for (var i = 0; i < preMadeLoc.length; i++) {
                        db.transaction(execLoc(preMadeLoc[i]));
                    }

                    function execLoc(sqls) {
                        return function(tx) {
                            tx.executeSql('insert into locations (id, name, province, topography, classification, land_area, population, population_density, code) values(?, ?, ?, ?, ?, ?, ?, ?, ?)', sqls, function(tx, res) {
                                console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
                            });
                        };
                    }
                }).fadeIn(200);
            });

        }
    });
});

as you can see the only thing that my database location has is the first index of the array. nothing else.

Any help?

Was it helpful?

Solution

I think the issue is that the 'tx' data is not being passed into your function. I would try something like this:

Change this:

                for (var i = 0; i < preMadeLoc.length; i++) {
                    db.transaction(execLoc(preMadeLoc[i]));
                }

To:

db.transaction(function(tx) {
    for (var i = 0; i < preMadeLoc.length; i++) {
        tx.executeSql('insert into locations (id, name, province, topography, classification, land_area, population, population_density, code) values(?, ?, ?, ?, ?, ?, ?, ?, ?)', preMadeLoc[i]);
    }
});

This makes use of the transactions ability to handle multi SQL statements and ensures that the tx variable is accessible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top