0

真的很抱歉不得不在论坛上提问,但我整天都在努力解决这个问题。

我有一个 phonegap 应用程序,它创建一个 SQLite 数据库,创建一个表,从 tinternet 获取 JSON 提要,然后将该数据输入表中。

一切似乎都很好,直到应该填充数据库的位置。“insertProperty”函数只是不运行。任何帮助都会非常棒!

以下是相关的代码片段。

x$(document).on('deviceready', function () {

    // CREATE DATABASE
var localDatabase = window.sqlitePlugin.openDatabase("localDB", "1.0", "Local DB", 5000000);
console.log("Database Loaded");
// CREATE properties TABLE
var query = "CREATE TABLE IF NOT EXISTS properties (propertyid INT, propertytype text, address text,  postcode text, clientid INT);"
db.transaction(function (trxn) {
    trxn.executeSql(query,  [],  callback,
    function (transaction, error) { //error callback
        console.log(error);
    });
});
}



function insertProperty(db, data) {
var query = "INSERT INTO properties (propertyid, propertytype, address, postcode, clientid) VALUES (?,?,?,?,?);"
db.transaction(function (trxn) {
    trxn.executeSql(query,[data.propertyid, data.propertytype, data.address, data.postcode, data.clientid],
    function (transaction, resultSet) {
        console.log('success');
    },
    function (transaction, error) {
        console.log(error);
    }
    );
});
}




x$().xhr("http://myurl.com", { // GET JSON FEED

    async: true,callback: function () { try {
        var dataArray = JSON.parse(this.responseText); // PARSE JSON FEED
        console.log("Loading live data");
        dataArray.forEach(function (thedata) { // LOOP DATA
            console.log(thedata.address);
            insertProperty(localDatabase, thedata);
        });
         console.log("Rendering Live Data");
        renderDataset(dataArray)
    } catch (e) { // failed to retrieve data
            console.log("There was a problem, Loading local data");
            getStoredProperties(localDatabase, function (offlineData) {
                renderDataset(offlineData);
            });
         }
    }
});

该脚本运行至“console.log(thedata.address);” 并向我显示了一个地址,但是一旦它尝试运行 insertProperty 函数,它就会退出并直接进入“console.log(“出现问题,正在加载本地数据”);” 少量。

再次感谢您提供的任何帮助。担

4

1 回答 1

0

显然,您在localDatabase“deviceready”函数的范围内定义了局部变量,因此当您在 xhr 回调函数中循环数据时,它无法访问。

于 2012-10-24T20:34:58.777 回答