IDBObjectStore:deleteIndex() 方法
注意:此功能在 Web Workers 中可用。
deleteIndex() 方法屬於 IDBObjectStore 介面,用於在資料庫連線期間銷燬指定名稱的索引,該方法用於版本升級過程中。
請注意,此方法只能從 VersionChange 事務模式的回撥中呼叫。請注意,此方法會同步修改 IDBObjectStore.indexNames 屬性。
語法
js
deleteIndex(indexName)
引數
indexName-
要移除的現有索引的名稱。
返回值
無(undefined)。
異常
InvalidStateErrorDOMException-
如果方法不是從
versionchange事務模式的回撥中呼叫的,則會丟擲此異常。 TransactionInactiveErrorDOMException-
如果此
IDBObjectStore所屬的事務不是活動的(例如,已被刪除或移除),則會丟擲此異常。 NotFoundErrorDOMException-
如果資料庫中不存在具有給定名稱(區分大小寫)的索引,則會丟擲此異常。
示例
在以下示例中,您可以看到 onupgradeneeded 處理程式用於在載入版本號更高的資料庫時更新資料庫結構。 IDBObjectStore.createIndex 用於在物件儲存上建立新索引,之後我們使用 deleteIndex() 刪除不再需要的舊索引。有關完整的可執行示例,請參閱我們的 待辦事項通知 應用(線上檢視示例)。
js
let db;
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
// these two event handlers act on the database being opened successfully, or not
DBOpenRequest.onerror = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Error loading database.";
};
DBOpenRequest.onsuccess = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Database initialized.";
// store the result of opening the database in the db variable. This is used a lot below
db = event.target.result;
// Run the displayData() function to populate the task list with all the to-do list data already in the IDB
displayData();
};
// This event handles the event whereby a new version of the database needs to be created
// Either one has not been created before, or a new version number has been submitted via the
// window.indexedDB.open line above
// it is only implemented in recent browsers
DBOpenRequest.onupgradeneeded = (event) => {
const db = event.target.result;
db.onerror = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Error loading database.";
};
// Create an objectStore for this database
const objectStore = db.createObjectStore("toDoList", {
keyPath: "taskTitle",
});
// define what data items the objectStore will contain
objectStore.createIndex("hours", "hours", { unique: false });
objectStore.createIndex("minutes", "minutes", { unique: false });
objectStore.createIndex("day", "day", { unique: false });
objectStore.createIndex("month", "month", { unique: false });
objectStore.createIndex("year", "year", { unique: false });
objectStore.createIndex("notified", "notified", { unique: false });
objectStore.deleteIndex("seconds");
objectStore.deleteIndex("contact");
};
規範
| 規範 |
|---|
| Indexed Database API 3.0 # ref-for-dom-idbobjectstore-deleteindex① |
瀏覽器相容性
載入中…
另見
- 使用 IndexedDB
- 開始事務:
IDBDatabase - 使用事務:
IDBTransaction - 設定鍵的範圍:
IDBKeyRange - 檢索和修改資料:
IDBObjectStore - 使用遊標:
IDBCursor - 參考示例:待辦事項通知(檢視即時示例)。