2012-10-24 6 views
0

정말 유감스럽게도 포럼에서 질문하고 싶지만 하루 종일이 문제를 해결하기 위해 노력하고 있습니다.Phonegap SQLite 테이블에 쓰기

SQLite 데이터베이스를 만들고 테이블을 만들고 tinternet에서 JSON 피드를 가져온 다음 해당 데이터를 테이블에 공급하는 phonegap 앱이 있습니다.

데이터베이스가 채워 져야하는 비트까지 모든 것이 정상적인 것처럼 보입니다. "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 ("로컬 데이터로드 중 문제가 발생했습니다.); " 비트.

제공 할 수있는 도움에 다시 한 번 감사드립니다. Dan

답변

0

"deviceready"기능의 범위에 로컬 변수 localDatabase을 정의하고 있으므로 xhr 콜백 함수 내에서 데이터를 루핑 할 때 액세스 할 수 없습니다.

+0

AWESOME! 감사 Zvona, 나는 지금 바보 같은 느낌! 아직도, 적어도 지금은 내가 var.LocalDatabase 대신 window.localDatabase를 선언했다 ... – Dan

+0

그냥 한 번만 더 질문 Zvona - 이제 스크립트가 잘 실행되지만 insertProperty 함수를 호출 할 때 오류가 발생합니다. { "message": "SQL 문 오류 : 해당 테이블이 없습니다 : 속성"} 테이블이 생성되지는 않았지만 이유를 모르겠습니다 ... 트랜잭션을 localDatabase.transation (txrn) {하지만 그건 도움이되지 못했습니다 ... – Dan

+0

deviceready 콜백 내에서 모든 함수 (심지어 x $(). xhr)를 실행해야합니다. 이제 XHR을 직접 실행하고 있으며, 디바이스가 생성되기 전에 콜백이 실행될 수 있습니다. – zvona

관련 문제