我創建了一個小的Phonegap應用程序,通過XML從Ajax調用中獲取新聞數據。這工作正常,但我想將數據存儲在數據庫表中以允許離線閱讀新聞。使用循環將行添加到sqlite數據庫(Phonegap)
所以當Ajax回調循環遍歷數據時,我用它填充全局新聞對象,然後調用一個函數來檢查數據是否已經存儲在數據庫中。如果不是,則應該將其插入到數據庫新聞表中。
的問題是,在交易的消息存儲在表,好像我的新聞物不存在了,因爲我得到的消息:
Uncaught TypeError: Cannot read property 'title' of undefined in ...
所以,我怎麼能確保這項工作?下面是部分的代碼,我選擇了新聞,要檢查,如果它已經存在:
// 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;
}
}
}
的newsContainer是一個充滿數據的幾行,我已經檢查了。如果有人能幫助我理解爲什麼這不起作用,我會非常高興。
在此先感謝!
問候,
貝恩德
請問您能否發出示例代碼,我仍然無法弄清楚 –