IDBDatabase
接口在 IndexedDB API 提供
连接到数据库
; you can use an
IDBDatabase
object to open a
transaction
on your database then create, manipulate, and delete objects (data) in that database. The interface provides the only way to get and manage versions of the database.
注意 : Everything you do in IndexedDB always happens in the context of a transaction , representing interactions with data in the database. All objects in IndexedDB — including object stores, indexes, and cursors — are tied to a particular transaction. Thus, you cannot execute commands, access data, or open anything outside of a transaction.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="../API/EventTarget.html" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/IDBDatabase" target="_top"><rect x="151" y="1" width="110" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="206" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">IDBDatabase</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
IDBDatabase.name
只读
DOMString
that contains the name of the connected database.
IDBDatabase.version
只读
IDBDatabase.objectStoreNames
只读
DOMStringList
that contains a list of the names of the
对象存储
currently in the connected database.
继承自: EventTarget
IDBDatabase.close()
Returns immediately and closes the connection to a database in a separate thread.
IDBDatabase.createMutableFile()
Creates a file handle, allowing files to be stored inside an IndexedDB database.
IDBDatabase.createObjectStore()
Creates and returns a new object store or index.
IDBDatabase.deleteObjectStore()
Destroys the object store with the given name in the connected database, along with any indexes that reference it.
IDBDatabase.transaction()
IDBTransaction
) containing the
IDBTransaction.objectStore
method, which you can use to access your object store. Runs in a separate thread.
监听这些事件使用
addEventListener()
或通过把事件监听器赋值给
on
eventname
特性为此接口。
abort
onabort
特性。
close
onclose
特性。
error
onerror
特性。
versionchange
onversionchange
特性。
In the following code snippet, we open a database asynchronously (
IDBFactory
), handle success and error cases, and create a new object store in the case that an upgrade is needed (
IDBdatabase
). For a complete working example, see our
待办通知
app (
实时查看范例
)。
// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);
// these two event handlers act on the IDBDatabase object,
// when the database is opened successfully, or not
DBOpenRequest.onerror = function(event) {
note.innerHTML += '<li>Error loading database.</li>';
};
DBOpenRequest.onsuccess = function(event) {
note.innerHTML += '<li>Database initialised.</li>';
// store the result of opening the database in the db
// variable. This is used a lot later on
db = DBOpenRequest.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
DBOpenRequest.onupgradeneeded = function(event) {
var db = event.target.result;
db.onerror = function(event) {
note.innerHTML += '<li>Error loading database.</li>';
};
// Create an objectStore for this database using
// IDBDatabase.createObjectStore
var 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 });
note.innerHTML += '<li>Object store created.</li>';
};
This next line opens up a transaction on the Database, then opens an object store that we can then manipulate the data inside of.
var objectStore = db.transaction('toDoList', 'readwrite').objectStore('toDoList');
| 规范 | 状态 | 注释 |
|---|---|---|
|
索引数据库 API 2.0
在该规范中的 IDBDatabase 定义。 |
推荐 | 初始版本 |
|
索引数据库 API 草案
在该规范中的 IDBDatabase 定义。 |
推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
IDBDatabase
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
abort
event
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
close
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
close
event
|
Chrome 31 | Edge ≤18 | Firefox 50 | IE ? | Opera Yes | Safari 10.1 | WebView Android Yes | Chrome Android 31 | Firefox Android 50 | Opera Android Yes | Safari iOS 10.3 | Samsung Internet Android 2.0 |
createObjectStore
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
deleteObjectStore
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
error
event
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
名称
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
objectStoreNames
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
onabort
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
onclose
|
Chrome
31
|
Edge ≤18 | Firefox 50 | IE ? | Opera Yes | Safari 10.1 | WebView Android Yes | Chrome Android 31 | Firefox Android 50 | Opera Android Yes | Safari iOS 10.3 | Samsung Internet Android 2.0 |
onerror
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
onversionchange
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
transaction
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
version
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
versionchange
event
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android 25 | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android 1.5 |
| Available in workers |
Chrome
Yes
|
Edge ≤18 | Firefox 37 | IE ? | Opera Yes | Safari ? |
WebView Android
Yes
|
Chrome Android
Yes
|
Firefox Android 37 | Opera Android Yes | Safari iOS ? |
Samsung Internet Android
Yes
|
完整支持
部分支持
兼容性未知
见实现注意事项。
要求使用供应商前缀或不同名称。
IDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
IDBDatabase
IDBCursor
IDBCursorSync
IDBCursorWithValue
IDBDatabaseException
IDBDatabaseSync
IDBEnvironment
IDBEnvironmentSync
IDBFactory
IDBFactorySync
IDBIndex
IDBIndexSync
IDBKeyRange
IDBObjectStore
IDBObjectStoreSync
IDBOpenDBRequest
IDBRequest
IDBTransaction
IDBTransactionSync
IDBVersionChangeEvent
IDBVersionChangeRequest