2

我需要创建一个 HTML5 Web SQL 数据库(我意识到它已被弃用)以在 iPad 上使用。过去在 iOS 6 中,您可以立即创建一个 50 MB 的数据库。但是现在 iOS 7 引入了一个 bug(见这篇文章) 阻止创建 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 上运行良好。但是,当数据库开始很小时,此代码将不再工作。感谢您的时间和帮助!

谢谢,

安迪

4

2 回答 2

1

好的,我想出了一种方法来做到这一点。这有点草率,但它有效。在下面的代码中,我正在测试 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 错误,但与此同时,也许这可以帮助其他人。

谢谢,

安迪

于 2013-10-02T20:57:43.617 回答
1

您应该能够使用规范中定义的事务方法的错误回调。

database.transaction(
    function (tx) {
        tx.executeSql('INSERT INTO mytable (x, y) VALUES (?, ?)', [x, y], function() {}, function() {});
    }, 
    function (error) {
        if (error.code === 4) {
            // Quota exceeded
        }
    }
);

在 iOS 6 和 7 中,我总是在提示用户增加数据库大小后看到使用 QUOTA_ERR 代码调用的错误回调。如果数据库写入超过 50 MB 的硬限制,您也会看到同样的错误。

我不确定是否有一种很好的方法来确定您是否看到数据库达到硬限制,或者是否提示用户增加数据库大小。如果用户增加 DB 大小或不允许它增加,您将看到同样的错误。

于 2013-11-07T21:25:35.697 回答