createObjectStore() 方法在 IDBDatabase interface creates and returns a new object store or index.

The method takes the name of the store as well as a parameter object that lets you define important optional properties. You can use the property to uniquely identify individual objects in the store. As the property is an identifier, it should be unique to every object, and every object should have that property.

This method can be called only versionchange transaction.

注意: 此特征可用于 Web 工作者 .

句法

IDBDatabase.createObjectStore(name);
IDBDatabase.createObjectStore(name, options);
					

参数

名称

The name of the new object store to be created. Note that it is possible to create an object store with an empty name.

optionalParameters 可选

An options object whose attributes are optional parameters to the method. It includes the following properties:

属性 描述
keyPath key path to be used by the new object store. If empty or not specified, the object store is created without a key path and uses out-of-line keys . You can also pass in an array as a keyPath .
autoIncrement true , the object store has a key generator 。默认为 false .

Unknown parameters are ignored.

返回

新的 IDBObjectStore .

异常

此方法可能引发 DOMException 采用 DOMError of one of the following types:

异常 描述
InvalidStateError Occurs if the method was not called from a versionchange transaction callback. For older WebKit browsers, you must call first.
TransactionInactiveError Occurs if a request is made on a source database that doesn't exist (e.g. has been deleted or removed.) In Firefox previous to version 41, an InvalidStateError was raised in this case as well, which was misleading; this has now been fixed (see bug 1176165 )。
ConstraintError An object store with the given name (based on case-sensitive comparison) already exists in the connected database.
I nvalidAccessError autoIncrement is set to true and keyPath is either an empty string or an array containing an empty string.

范例

// Let us open our database
var request = window.indexedDB.open("toDoList", 4);
// This handler is called when a new version of the database
// is created, either when one has not been created before
// or when a new version number is submitted by calling
// window.indexedDB.open().
// This handler is only supported in recent browsers.
request.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
  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>";
};
					

规范

规范 状态 注释
索引数据库 API 2.0
The definition of 'createObjectStore()' in that specification.
推荐
索引数据库 API 草案
The definition of 'createObjectStore()' in that specification.
推荐

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
createObjectStore Chrome 24
24
23 — 24 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16 Prefixed
Prefixed Implemented with the vendor prefix: moz
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

图例

完整支持

完整支持

部分支持

部分支持

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: