我想在设备在线时将数据插入数据库,如果设备离线,我想显示存储在数据库中的数据。
目前我在这里解析来自 rss 提要的数据并在设备在线时将数据插入数据库中,当设备离线时我无法显示相同的数据。而且我尝试了很多次离线显示数据,使用以下代码:
document.addEventListener("offline", yourCallbackFunction, false);
$.each(data.responseData.feed.entries, function (e, item) {
var titles=item.title;
var linked=item.link;
s += '<li><div class="itemTitle"><a href="' + item.link + '" target="' + def.TitleLinkTarget + '" >' + item.title + "</a></div>";
if (def.ShowPubDate) {
i = new Date(item.publishedDate);
s += '<div class="itemDate">' + i.toLocaleDateString() + "</div>";
}
if (def.ShowDesc) {
if (def.DescCharacterLimit > 0 && item.content.length > def.DescCharacterLimit) {
s += '<div class="itemContent">' + item.content.substr(0, def.DescCharacterLimit) + "...</div>";
}
else {
s += '<div class="itemContent">' + item.content + "</div>";
console.log(s);
}
}
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Populate the database
//
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (title, desc)');
tx.executeSql('INSERT INTO DEMO (title, desc) Values(?,?)',[titles,s]);
}
// Query the database
//
function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
// Query the success callback
//
function querySuccess(tx, results) {
var len = results.rows.length;
console.log("DEMO table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " ID = " + results.rows.item(i).title + " Data = " + results.rows.item(i).desc);
$("#apps ul").append('<li><a href="results.rows.item(i).desc"><span class="tab">' +results.rows.item(i).desc+'</span></a></li>');
}
}
document.addEventListener("offline", querySuccess, false);