IDBFactory
インターフェイスの open()
メソッドは、データベースへの接続を開くことを要求します。
このメソッドは即座に IDBOpenDBRequest
オブジェクトを返し、そして非同期でデータベースを開きます。操作が成功した場合、このメソッドから返される request オブジェクトに result の属性として接続のための新しい IDBDatabase
オブジェクトが設定されて、 success イベントが発生します。
データベースとの接続の間にエラーが発生した場合、このメソッドで返される request オブジェクトで、 error イベントが発生します。
upgradeneeded
, blocked
, versionchange
イベントが発生することもあります。
構文
現在の標準では:
var IDBOpenDBRequest = indexedDB.open(name); var IDBOpenDBRequest = indexedDB.open(name, version);
引数
- name
- データベースの名前。
- version Optional
- 省略可。データベースを開くバージョン。バージョンが提供されずにデータベースが存在した場合、データベースへの接続はバージョンを変更せずに開かれます。バージョンが提供されず、データベースも存在しなかった場合、バージョン番号
1
が生成されます。
実験的な Gecko の options オブジェクト
- options (version および storage) Optional
- Gecko では、バージョン 26 から、標準外の
options
オブジェクトをIDBFactory.open
の引数として指定することができ、データベースのversion
番号と、加えてストレージにpersistent
(永続的) またはtemporary
(一時的) のどちらを使用したいかを指定する storage 値を指定することができます。警告:storage
属性は非推奨であり、まもなく Gecko から削除される予定です。永続的なストレージを得るには、代わりにStorageManager.persist()
を使用してください。
メモ: 利用可能な様々なストレージ種別における詳細情報や、 Firefox がクライアント側データストレージを扱う方法については、 ブラウザーのストレージ制限と削除基準で見つけることができます。
返値
この要求に関連のある連続したイベントが発生する IDBOpenDBRequest
オブジェクト。
例外
このメソッドは、次の型のような DOMError を持つ DOMException
が発生する可能性があります。
例外 | 説明 |
---|---|
TypeError |
バージョンの値がゼロかマイナスの値、または数値でない場合。 |
例
open
を現在の仕様書の version
引数を付けて呼び出す例です。
var request = window.indexedDB.open("toDoList", 4);
次のコードスニペットは、データベースを開く要求をして、成功の場合と失敗の場合のイベントハンドラを登録しています。完璧に動作する例は、 To-do Notifications app (view example live.) を見てください。
var note = document.querySelector("ul");
// In the following line, you should include the prefixes
// of implementations you want to test.
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
// DON'T use "var indexedDB = ..." if you're not in a function.
// Moreover, you may need references to some window.IDB* objects:
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
// (Mozilla has never prefixed these objects, so we don't
// need window.mozIDB*)
// Let us open version 4 of our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);
// these two event handlers act on the database being 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, for opening
// transactions and suchlike.
db = DBOpenRequest.result;
};
仕様書
ブラウザーの対応
BCD tables only load in the browser
このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 https://github.com/mdn/browser-compat-data をチェックアウトしてプルリクエストを送信してください。
関連情報
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications (view example live.)