2012-11-24 38 views
5

Tôi đã tạo một ứng dụng Phonegap nhỏ nhận dữ liệu tin tức từ cuộc gọi Ajax qua XML. Điều này làm việc tốt, nhưng tôi muốn lưu trữ dữ liệu trong một bảng cơ sở dữ liệu cũng cho phép đọc ngoại tuyến tin tức.Thêm hàng vào cơ sở dữ liệu sqlite bằng cách sử dụng vòng lặp (Phonegap)

Vì vậy, khi gọi lại Ajax lặp qua dữ liệu, tôi điền một đối tượng tin tức toàn cầu với nó và sau đó gọi hàm để kiểm tra xem dữ liệu đã được lưu trữ trong cơ sở dữ liệu hay chưa. Nếu không, nó sẽ được chèn vào bảng tin cơ sở dữ liệu.

Vấn đề là trong các giao dịch để lưu trữ các thông tin trong bảng, nó có vẻ như Object tin tức của tôi không được trình bày nữa vì tôi nhận được thông báo:

Uncaught TypeError: Cannot read property 'title' of undefined in ...

Vậy làm thế nào tôi có thể chắc chắn rằng công việc này? Đây là mã của phần nơi tôi chọn những tin tức và muốn kiểm tra xem nó đã có:

// Check if a news from the internet already exists in the database; If not, insert it 
function checkNewsInDB(){ 
    db.transaction(function(tx){ 
     tx.executeSql("SELECT * FROM NEWS", [], checkSuccess, dbErrorCB); 
    }, dbErrorCB, dbSuccessCB); 
} 

// Result Callback from the News Check 
function checkSuccess(ctx, result){ 
    var len = result.rows.length; 
    var found = false; 
    for(var n = 0; n < newsContainer.length; n++){ 
     for(var r = 0; r < len; r++){ 
      if(result.rows.item(r).n_title == newsContainer[n].title 
       && result.rows.item(r).n_pubdate == newsContainer[n].pubdate){ 
       found = r; 
      } 
     } 
     if(found == false){ 
      db.transaction(function(tx){ 
       tx.executeSql("INSERT INTO NEWS (n_title, n_link, n_creator, n_pubdate, n_description) VALUES (?,?,?,?,?)", [newsContainer[n].title, newsContainer[n].link, newsContainer[n].creator, newsContainer[n].pubdate, newsContainer[n].description], insertSuccess, dbErrorCB); 
      }, dbErrorCB, dbSuccessCB); 
     } else { 
      found = false; 
     } 
    } 
} 

Các newsContainer IS đầy với một vài dòng dữ liệu, tôi đã kiểm tra đó. Tôi sẽ rất hạnh phúc nếu ai đó có thể giúp tôi hiểu tại sao điều này không hiệu quả.

Cảm ơn trước!

Greetings,

Bernd

Trả lời

4

db.transaction là không đồng bộ - do thời gian executeSql thực sự chạy, n đã được tăng lên để kết thúc vòng lặp.

Thay vì tạo giao dịch mới cho từng mục, hãy thử di chuyển vòng lặp bên trong chức năng giao dịch.

+0

Bạn có thể gửi mã mẫu không, tôi vẫn không thể hiểu được –

2

Cảm ơn câu trả lời. Dưới đây là mã hoạt động cho tất cả những người có cùng vấn đề:

// Check if a news from the internet already exists in the database; If not, insert it 
function checkNewsInDB(){ 
    db.transaction(function(tx){ 
     tx.executeSql("SELECT * FROM NEWS", [], checkSuccess, dbErrorCB); 
    }, dbErrorCB, dbSuccessCB); 
} 

// Result Callback from the News Check 
function checkSuccess(ctx, result){ 
    var len = result.rows.length; 
    var found = false; 
    for(var n = 0; n < newsContainer.length; n++){ 
     for(var r = 0; r < len; r++){ 
      if(result.rows.item(r).n_title == newsContainer[n].title 
       && result.rows.item(r).n_pubdate == newsContainer[n].pubdate){ 
       found = r; 
      } 
     } 
     if(found == false){ 
      var title = newsContainer[n].title; 
      var link = newsContainer[n].link; 
      var creator = newsContainer[n].creator; 
      var pubdate = newsContainer[n].pubdate; 
      var description = newsContainer[n].description; 
      ctx.executeSql("INSERT INTO NEWS (n_title, n_link, n_creator, n_pubdate, n_description) VALUES (?,?,?,?,?)", 
         [title, link, creator, pubdate, description], insertSuccess, dbErrorCB); 
     } else { 
      found = false; 
     } 
    } 
} 
Các vấn đề liên quan