2016-09-27 90 views
0

我正在使用Ionic v1並嘗試將一些JSON記錄插入到Android Kitkat上的SQLite數據庫中。如果我使用一個靜態變量,它的工作原理,但只是插入數組的第一個項目,但如果我試圖讓變量名動我得到一個錯誤:Sqlite沒有插入動態JSON數據

Cannot read property 'mac_id' of undefined

我已經嘗試了以下兩個代碼段,第一個作品,但僅插入JSON數組中的第一項,第二不工作:

作品:

但是,只有插入第一項(多次)

for (var i = 0; i < data.length; i++) 
       { 

        var mac_id = data[i].mac_id; 
        var device_type = data[i].device_type; 
        var machine_id = data[i].machine_id; 
        var machine_name = data[i].machine_name; 
        var date_created = data[i].date_created; 

        db.transaction(function (tx) { 

         var query = "INSERT INTO devices (mac_id, device_type,machine_id,machine_name,date_created) VALUES (?,?,?,?,?)"; 

         tx.executeSql(query, [mac_id , device_type , machine_id , machine_name , date_created ], function(tx, res) { 

         }, 
         function(tx, error) { 
          console.log(' device INSERT error: ' + error.message); 
         }); 
        }, function(error) { 
         console.log(' device transaction error: ' + error.message); 
        }, function() { 
         console.log('device INSERT ok'); 
        });     

       } 

不工作

for (var i = 0; i < data.length; i++) 
       { 


        db.transaction(function (tx) { 

         var query = "INSERT INTO devices (mac_id, device_type,machine_id,machine_name,date_created) VALUES (?,?,?,?,?)"; 

         tx.executeSql(query, [data[i].mac_id, data[i].device_type, data[i].machine_id, data[i].machine_name, data[i].date_created ], function(tx, res) { 

         }, 
         function(tx, error) { 
          console.log(' device INSERT error: ' + error.message); 
         }); 
        }, function(error) { 
         console.log(' device transaction error: ' + error.message); 
        }, function() { 
         console.log('device INSERT ok'); 
        });     

       } 

JSON陣列

[ 
    { 
    "id": "3", 
    "mac_id": "fsdf324324", 
    "device_type": "redvfsdfds", 
    "machine_id": "3", 
    "machine_name": "sdfsdfsdf", 
    "date_created": "3322342" 
    }, 
    { 
    "id": "2", 
    "mac_id": "243434", 
    "device_type": "fredssd", 
    "machine_id": "2", 
    "machine_name": "fdsfsdf", 
    "date_created": "43434" 
    }, 
    { 
    "id": "1", 
    "mac_id": "1324324234", 
    "device_type": "bweight", 
    "machine_id": "1", 
    "machine_name": "dffdgf", 
    "date_created": "4324234" 
    } 
] 

回答

0
db.transaction(function (tx) { 
        for (var i = 0; i < $scope.data.length; i++) 
        { 
        var query = "INSERT INTO devices (mac_id, device_type,machine_id,machine_name,date_created) VALUES (?,?,?,?,?)"; 

        tx.executeSql(query, [data[i].mac_id, data[i].device_type, data[i].machine_id, data[i].machine_name, data[i].date_created ], function(tx, res) { 
        },function(tx, error) { 
         console.log(' device INSERT error: ' + error.message); 
        }); 
       } 
       }, function(error) { 
        console.log(' device transaction error: ' + error.message); 
       }, function() { 
        console.log('device INSERT ok'); 
       }); 

您應該使用內幕交易循環。像這樣使用它。希望這有助於:)