変数 = 【IDBDatabase】.createObjectStore( 名前, 連想配列 );
【IDBObjectStore】.createIndex(名前, キー名, 連想配列 );
【IDBObjectStore】.deleteObjectStore( 名前 );作成済みのオブジェクトストアを削除する場合には、deleteObjectStoreを呼び出します。引数には、削除するオブジェクトストアの名前を指定します。これは、createObjectStoreで指定した名前を利用すればよいでしょう。
変数 =【IDBDatabase】.transaction( 引数指定 ).objectStore("mydata");既にあるオブジェクトストアを取り出すときにはちょっと注意が必要です。IDBDatabaseには「指定のオブジェクトストアを取り出す」というメソッドはないのです。ではどうするのかというと、まずIDBDatabaseから「IDBTransaction」オブジェクトを取得し、そこから「objectStore」メソッドで指定のオブジェクトストアのオブジェクトを取り出す、という形になります。
var request = db.setVersion("1.0");これは、IDBVersionChangeRequestの取得をするものです。データベース利用の際には、まずsetVersionでバージョンの設定を行います。そしてこれが成功したなら、onsuccessプロパティに設定されている関数を呼び出し処理をします。つまり、ここにオブジェクトストア取得のための処理を用意しておけばいいわけです。
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sample</title> <style> h1 {font-size: 14pt; font-weight:bold; background-color:#DDF; padding:3px; } #ftable tr td { background-color:#CFC; } #datas tr td { background-color:#FCC; } </style> <script type="text/javascript"> var IDBTransaction; var IndexedDB; var db; var objectStore; function initial(){ IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; try { IndexedDB = mozIndexedDB; } catch(e){} try { IndexedDB = webkitIndexedDB; } catch(e){} var request = IndexedDB.open("MyTestDatabase"); request.onerror = function(event) { alert("ERROR!"); }; request.onsuccess = function(event) { db = request.result; createObjectStoreMethod(); alert("OK!"); }; } function createObjectStoreMethod(){ var request = db.setVersion("1.0"); request.onerror = function(event) { // ERROR処理 }; request.onsuccess = function(event) { try { objectStore = db.createObjectStore("mydata", { keyPath: "name" }); objectStore.createIndex("name", "name", { unique: true }); }catch(e){} }; } </script> </head> <body onload="initial();"> <h1>Index DB Sample.</h1> </body> </html>
<< 前へ | 次へ >> |