2014-02-18 44 views
0

我試圖從某個數組查詢循環。但它只保存數組的第一個元素。我查看了console.log(),它已經爲每個執行返回了1。SQLite Phonegap:查詢循環錯誤

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擁有的唯一的事情是數組的第一個索引。沒有其他的。

任何幫助?

回答

0

我認爲問題在於'tx'數據沒有被傳遞到你的函數中。我會嘗試這樣的事:

更改此:

要:

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變量訪問。