2011-02-23 4 views
0

Blackberry Webworks 웹 응용 프로그램이 BB5에서 실행되며 SQLite 데이터베이스를 사용하여 데이터를 로컬에 저장합니다. 데이터베이스에는 이벤트 및 항공편이라는 두 개의 테이블이 있으며, 하나의 이벤트에 여러 개의 항공편이 연결될 수 있습니다.Blackberry Webworks에서 SQLite를 사용하여 관련 데이터 삽입

데이터 배열에서 두 테이블을 채우는 방법을 찾기가 어렵습니다. 내 문제는 BB의 SQLite 구현이 비동기 방식으로 작동하기 때문에 외래 키를 flight 테이블에 삽입하는 것입니다.

db.transaction(function(tx) { 
    for(var i = 0; i < eventsArray.length; i++) { 
      var insertId = 0; 
      tx.executeSql("INSERT INTO event(id,eventName,venueName) VALUES (?,?,?)", 
          [null, 
          eventsArray[i].eventName, 
          eventsArray[i].venueName], 
          function(tx,result) { //success callback 
           insertId = result.insertId; 
           //If I try inserting flights here, eventsArray[i] always returns 
           //the last item in the array, the for loop has kept running 
          } 
      ); 
      //If I try inserting here, I don't have the insertId 
      //to populate the foreign key (still set to 0 as the 
      //callbacks haven't fired yet) 
} 

비행 물에 대한 삽입 쿼리를 수행하려고 시도 할 때마다 데이터 조각이 누락되었습니다. 인서트 ID 또는 삽입해야하는 항공편이 포함 된 실제 이벤트 객체입니다.

더 좋은 방법이 있나요?

답변

0

이 문제를 해결할 수있는 방법은 여러 가지가 있습니다. 직접 접근 :

db.transaction(function(tx) { 
    var eventIds = []; 
    for(var i = 0; i < eventsArray.length; i++) {
tx.executeSql("INSERT INTO event(id,eventName,venueName) VALUES (?,?,?)", [null, eventsArray[i].eventName, eventsArray[i].venueName], function(tx,result) { //success callback eventIds[i] = result.insertId; } ); } //for //now, for every event eventsArray[i] you have eventId as eventIds[i] for(i = 0; i < flightsArray.length; i++) { //use eventIds[i] here } });

id를 콜백에 eventsArray [i] .id = result.indsertId로 저장하는 것이 더 적절합니다. 즉 현재 이벤트 객체에 직접 적용됩니다. 하지만 비즈니스 로직의 세부 사항에 달려 있습니다. event.id는 이미 다른 것을 의미합니다. 또한 이벤트에 로컬 변수를 사용하면 멤버에 액세스 할 때마다 eventsArray [i]를 다시 계산하지 않아도됩니다.

관련 문제