Html 如何从 JavaScript 中删除整个 IndexedDB 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15861630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:18:46  来源:igfitidea点击:

How can I remove a whole IndexedDB database from JavaScript?

javascripthtmlphantomjsindexeddb

提问by aknuds1

How can one remove a whole IndexedDB database from JavaScript, as opposed to just an object store? I'm using the IndexedDB shim, which may use WebSQL as its backend.

如何从 JavaScript 中删除整个 IndexedDB 数据库,而不仅仅是对象存储?我正在使用IndexedDB shim,它可能使用 WebSQL 作为其后端。

I'd mainly like to know how to do this for the PhantomJS (headless) browser, although Chrome, Safari (on iPad) and IE10 are other important browsers.

我主要想知道如何为 PhantomJS(无头)浏览器执行此操作,尽管 Chrome、Safari(在 iPad 上)和 IE10 是其他重要的浏览器。

回答by aknuds1

As far as I can tell, one should use indexedDB.deleteDatabase:

据我所知,应该使用indexedDB.deleteDatabase

var req = indexedDB.deleteDatabase(databaseName);
req.onsuccess = function () {
    console.log("Deleted database successfully");
};
req.onerror = function () {
    console.log("Couldn't delete database");
};
req.onblocked = function () {
    console.log("Couldn't delete database due to the operation being blocked");
};

I can confirm that it works with PhantomJS 1.9.0 and Chrome 26.0.1410.43.

我可以确认它适用于 PhantomJS 1.9.0 和 Chrome 26.0.1410.43。

回答by CYoung

I found that the following code works OK but to see the DB removed in the Chrome Resources Tab I have had to refresh the page. Also I found I had problems with the Chrome debug tools running while doing transactions. Makes it harder to debug but if you close it while running code the code seems to work OK. Significant also is to set a reference to the object store when opening the page. Obviously the delete part of the code is in the deleteTheDB method.

我发现以下代码可以正常工作,但要查看 Chrome 资源选项卡中删除的数据库,我必须刷新页面。我还发现我在处理事务时运行 Chrome 调试工具时遇到了问题。使调试变得更加困难,但是如果您在运行代码时关闭它,代码似乎可以正常工作。重要的还有在打开页面时设置对对象存储的引用。显然,代码的删除部分在 deleteTheDB 方法中。

Code derived from example provided by Craig Shoemaker on Pluralsight.

代码源自 Craig Shoemaker 在 Pluralsight 上提供的示例。

var IndDb = {
    name: 'SiteVisitInsp',
    version: 1000,
    instance: {},
    storenames: {
        inspRecords: 'inspRecords',
        images: 'images'
    },
    defaultErrorHandler: function (e) {
        WriteOutText("Error found : " + e);
    },
    setDefaultErrorHandler: function (request) {
        if ('onerror' in request) {
            request.onerror = db.defaultErrorHandler;
        }
        if ('onblocked' in request) {
            request.onblocked = db.defaultErrorHandler;
        }
    }

};

var dt = new Date();
var oneInspRecord =
        {            
            recordId: 0,
            dateCreated: dt,
            dateOfInsp: dt,
            weatherId: 0,
            timeArrived: '',
            timeDeparted: '',
            projectId: 0,
            contractorName: '',
            DIWConsultant: '',
            SiteForeman: '',
            NoOfStaffOnSite: 0,
            FileME: '',
            ObservationNotes: '',
            DiscussionNotes: '',
            MachineryEquipment: '',
            Materials: ''
        };

var oneImage =
{
    recordId: '',
    imgSequence: 0,
    imageStr: '',
    dateCreated: dt
}


var SVInsp = {
    nameOfDBStore: function () { alert("Indexed DB Store name : " + IndDb.name); },
    createDB: function () {
        openRequest = window.indexedDB.open(IndDb.name, IndDb.version);

        openRequest.onupgradeneeded = function (e) {
            var newVersion = e.target.result;
            if (!newVersion.objectStoreNames.contains(IndDb.storenames.inspRecords)) {
                newVersion.createObjectStore(IndDb.storenames.inspRecords,
                    {
                        autoIncrement: true

                    });
            }

            if (!newVersion.objectStoreNames.contains(IndDb.storenames.images)) {
                newVersion.createObjectStore(IndDb.storenames.images,
                    {
                        autoIncrement: true
                    });
            }
        };

        openRequest.onerror = openRequest.onblocked = 'Error'; //resultText;

        openRequest.onsuccess = function (e) {
            //WriteOutText("Database open");
            IndDb.instance = e.target.result;
        };

    },

    deleteTheDB: function () {
        if (typeof IndDb.instance !== 'undefined') {
            //WriteOutText("Closing the DB");

            IndDb.instance.close();
            var deleteRequest = indexedDB.deleteDatabase(IndDb.name)

            deleteRequest.onblocked = function () {
                console.log("Delete blocked.");
            }

            deleteRequest.onerror =
                function () {
                    console.log("Error deleting the DB");
                    //alert("Error deleting the DB");
                };
                //"Error deleting the DB";

            deleteRequest.onsuccess = function () {

                console.log("Deleted OK.");
                alert("*** NOTE : Requires page refresh to see the DB removed from the Resources IndexedDB tab in Chrome.");
                //WriteOutText("Database deleted.");

            };


        };

    }
}