我需要创建一个 HTML5 Web SQL 数据库(我意识到它已被弃用)以在 iPad 上使用。过去,在 iOS 6 中,您可以立即创建 50 MB 的数据库。但现在 iOS 7 引入了一个错误 (see this article),该错误阻止创建 50 MB 数据库。据我了解,此错误的解决方法是创建一个小型数据库并根据需要扩展它。 Safari 将提示用户增加 10 MB、25 MB 和 50 MB。但是,当提示用户增加数据库大小时,插入数据的 JavaScript 代码就会停止运行。我究竟做错了什么? (这种情况发生在 iOS 6 和 7 上。)据我所知,没有抛出任何错误。在用户提示增加数据库后,如何继续插入?我的 Web SQL 插入代码如下(为简洁起见,省略了一些代码):
db.transaction(function (tx) {
var insertSQL = 'insert into ...';
for (var i = 0; i < len; i++) {
var items = [{}]; // the items to insert
tx.executeSql(insertSQL, items, function (tx, results) {
// When all items are inserted, fire the callback method if there is one
if ((++itemsInserted === itemsToInsert) && $.isFunction(callback)) {
callback();
}
}, function (tx, error) {
if (console && $.isFunction(console.warn)) {
console.warn(error.message);
}
});
}
});
如果数据库最初创建时大小为 50 MB,则上述代码在 iOS 6 上运行良好。然而,当数据库一开始很小时,这段代码就不再起作用了。感谢您的宝贵时间和帮助!
谢谢
安迪
最佳答案
好的,我想出了一种方法来做到这一点。虽然有点草率,但是很有效。在下面的代码中,我正在测试 iOS 7,并通过 setInterval 启动计时器。如果数据在 15 秒的延迟后还没有进入本地表,那么我认为它不起作用,因为用户被增加数据库大小的提示阻止了。然后它重新尝试插入,第二次尝试应该会成功。
// loadData hits a server endpoint to retrieve data and inserts that data into the Web SQL database
function loadData(config) {
$.ajax({
url: 'the url',
//...other properties for the ajax request omitted...
success: function (data) {
// Kick off the insert of data into the local DB
insertLocalData();
// workaround for iOS 7 - verify whether the insert method's success callback was called
if (/iphone|ipad/i.test(navigator.userAgent) && navigator.userAgent.indexOf('Version/7') !== -1) {
config.timerId = setInterval(function () {
if (!config.dataInserted) {
insertLocalData();
}
else {
clearInterval(config.timerId);
}
}, 15000);
}
function insertLocalData() {
// db is a reference to the local DB and insert is the custom method whose guts can be found in the original question above
config.db.insert(config.TableName, data.Data, function () {
config.dataInserted = true;
// Execute callback function if one was given
if ($.isFunction(config.successCallback)) {
config.successCallback(config);
}
});
}
}
});
}
我希望 Apple 修复这个 Web SQL 错误,但同时这也许可以帮助其他人。
谢谢
安迪
关于javascript - 适用于 iOS 的 Web SQL 增长数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126034/