SQLite Phonegap:查询循环错误

时间:2014-02-18 16:04:10

标签: jquery cordova sqlite cordova-plugins

我正在尝试从某个数组查询for循环。但它只保存了数组的第一个元素。我查看了console.log(),它已经返回1中的1,以便执行。

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);
            });

        }
    });
});

你可以看到我的数据库location唯一拥有的是数组的第一个索引。没别的。

任何帮助?

1 个答案:

答案 0 :(得分:0)

我认为问题是'tx'数据没有传递到你的函数中。我会尝试这样的事情:

改变这个:

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

要:

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]);
    }
});

这利用事务处理多个SQL语句的能力,并确保tx变量可以访问。