我有一个 Apache Cordova 应用程序,用于缓存在没有可用通信(例如没有 3G 覆盖等)时无法发送到服务器的数据。
我有一些代码在设备恢复在线时运行。这段代码的想法是递归地遍历缓存的数据并将其发送到后台的服务器。发送后,应删除每一行以防止再次发送。
代码的精简和匿名版本如下......
var app = {
name: 'myApp',
displayName: 'my application',
db: null,
initialize: function () {
document.addEventListener("deviceready", app.onDeviceReady, false);
document.addEventListener("online", app.onDeviceOnline, false);
},
onDeviceOnline: function () {
app.db.transaction(
function (tx) {
app.uploadCachedData(tx);
},
function (error) {
console.error("'" + error.message + "' creating database transaction.");
},
function () {
}
);
},
onDeviceReady: function () {
app.db = window.openDatabase(app.name, "2.2", app.displayName, 1 * 1024 * 1024);
},
uploadCachedData: function (tx) {
tx.executeSql('select * from [data]', [],
function (tx, results) {
console.log("uploadCachedData, rows.length=" + results.rows.length);
if (results.rows.length > 0) {
var item = results.rows.item(0);
var cachedDataId = item.id;
console.log("Uploading cached data, id=" + cachedDataId)
app.ws.uploadImage(item.f1, item.f2, item.f3, item.f4, item.f5,
function (data) {
console.log("'Data' (id=" + cachedDataId + ") uploaded from cache.");
tx.executeSql('delete from [data] where [id] = ?', [cachedDataId],
function (tx, resultSet) {
console.log("'[data] where [id]=" + cachedDataId + "' deleted.");
app.uploadCachedData(tx);
},
function (error) {
console.log("'" + error.message + "' deleting [data] where [id]=" + cachedDataId + "'.");
});
},
function (response, textStatus, errorThrown) {
console.log("'" + error.message + "' uploading Data (id=" + cachedDataId + ") from cache.");
});
}
},
function (error) {
console.error("'" + error.message + "' selecting '[data]'.");
});
}
};
app.initialize();
当代码运行时,我看到消息最多包括“从缓存上传的‘数据’(id = 1)”,并且第一行数据被发送到服务器。我看不到更多消息,也看不到该消息后面的删除的成功或失败消息。我也没有看到更多消息表明已进行递归调用(我的测试数据中有 4 行),并且没有其他数据发送到服务器。
任何人都可以解释为什么删除似乎永远不会完成吗?
谢谢。
最佳答案
我还没找到原因。我必须假设这与我试图在与前一个选择相同的事务上执行删除这一事实有关。
通过创建一个新的 app.db.transaction 并从该新事务调用executeSql,我能够运行代码。
如果有人有更好的解决方案,我仍然会感兴趣。
关于javascript - Apache Cordova、SQLite、从 [表] 中删除,其中 id=?不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847587/