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

txu3uszq  于 2023-11-15  发布在  IndexedDB
关注(0)|答案(2)|浏览(340)

此问题在此处已有答案

How to delete indexedDB?(20个回答)
25天前关闭
如何从JavaScript中删除整个IndexedDB数据库,而不仅仅是一个对象存储?我使用的是IndexedDB shim,它可能使用WebSQL作为其后端。
我主要想知道如何为Android JS(无头)浏览器做到这一点,尽管Chrome,Safari(在iPad上)和IE 10是其他重要的浏览器。

3npbholx

3npbholx1#

据我所知,应该使用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");
};

字符串
我可以确认它可以与Android 1.9.0和Chrome 26.0.1410.43一起工作。

k10s72fa

k10s72fa2#

我发现下面的代码工作正常,但要看到DB在Chrome资源选项卡中删除,我不得不刷新页面。此外,我发现我在执行事务时运行Chrome调试工具时遇到问题。这使得调试更加困难,但如果您在运行代码时关闭它,代码似乎工作正常。重要的是在打开页面时设置对对象存储的引用。显然,代码在deleteTheDB方法中。
代码源自克雷格舒梅克在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.");

            };

        };

    }
}

字符串

相关问题