aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbdatabase/createobjectstore/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/idbdatabase/createobjectstore/index.html')
-rw-r--r--files/zh-cn/web/api/idbdatabase/createobjectstore/index.html178
1 files changed, 178 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbdatabase/createobjectstore/index.html b/files/zh-cn/web/api/idbdatabase/createobjectstore/index.html
new file mode 100644
index 0000000000..99ea2480df
--- /dev/null
+++ b/files/zh-cn/web/api/idbdatabase/createobjectstore/index.html
@@ -0,0 +1,178 @@
+---
+title: IDBDatabase.createObjectStore()
+slug: Web/API/IDBDatabase/createObjectStore
+translation_of: Web/API/IDBDatabase/createObjectStore
+---
+<p>{{ APIRef("IndexedDB") }}</p>
+
+<div>
+<p>{{domxref("IDBDatabase")}} 接口的 <strong><code>createObjectStore()</code></strong> 方法创建并返回一个新的 object store 或 index。</p>
+</div>
+
+<p>此方法接受一个参数作为 store 的名称,也接受一个可选的参数对象让你可以定义重要的可选属性。你可以用这个属性唯一的标识此 store 中的每个对象。因为参数是一个标识符,所以 store 中的每一个对象都应有此属性并保证此属性唯一。</p>
+
+<p>此方法只能在 <a href="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE"><code>versionchange</code></a> 事务中被调用。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">var <em>objectStore</em> = <em>IDBDatabase</em>.createObjectStore(<em>name</em>);
+var <em>objectStore</em> = <em>IDBDatabase</em>.createObjectStore(<em>name</em>, <em>options</em>);
+</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt>name</dt>
+ <dd>被创建的 object store 的名称。请注意创建空名称的 object store 是被允许的。</dd>
+ <dt>optionalParameters {{optional_inline}}</dt>
+ <dd>
+ <p>可选的对象,它的属性是此方法的可选参数,其中包括以下的属性:</p>
+
+ <table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Attribute</th>
+ <th scope="col">Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><code>keyPath</code></td>
+ <td>
+ <p><a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_keypath">key path</a> 被用在新的 object store 上。如果为空或未指定,object store 创建时将没有 key path,而是使用 <a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_outofline_key">out-of-line keys</a> 。你也能传一个数组作为 <code>keyPath</code> 。</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code>autoIncrement</code></td>
+ <td>如果为 <code>true</code>,  object store 有一个 <a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_keygenerator">key generator</a>. 默认为 <code>false</code>。</td>
+ </tr>
+ </tbody>
+ </table>
+
+ <p>未知参数将被忽略。</p>
+ </dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<dl>
+ <dt>{{domxref("IDBObjectStore")}}</dt>
+ <dd>新创建的 object store 对象。</dd>
+</dl>
+
+<h3 id="异常">异常</h3>
+
+<p>This method may raise a此方法可能会抛出一个 {{domxref("DOMException")}} 带有以下所列其中一种类型的 {{domxref("DOMError")}} :</p>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Exception</th>
+ <th scope="col">Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><code>InvalidStateError</code></td>
+ <td>
+ <p>在非<code>versionchange</code>事务中调用时发生。在一些旧版本的 Webkit 浏览器,你必须先调用{{APIRef("IDBVersionChangeRequest.setVersion")}}方法。</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code>TransactionInactiveError</code></td>
+ <td>
+ <p>如果对不存在的源数据库发出请求(例如,已被删除的)。此外,在 Firefox 版本小于 41 中,会抛出误导性的 <code>InvalidStateError</code> 错误,这一问题现已修复(请参阅 {{Bug("1176165")}})。</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code>ConstraintError</code></td>
+ <td>
+ <p>数据库中已存同名的对象存储(名字区分大小写)</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code><a href="/en-US/docs/IndexedDB/IDBDatabaseException#NON_TRANSIENT_ERR">I</a>nvalidAccessError</code></td>
+ <td>
+ <p>如果 <code>autoIncrement</code> 设置为 true,并且 keyPath 是空字符串或包含空字符串的数组。</p>
+ </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Example">Example</h2>
+
+<pre class="brush: js;highlight:[18]"> // 打开一个数据库
+ 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 += '&lt;li&gt;Error loading database.&lt;/li&gt;';
+ };
+
+ // 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 += '&lt;li&gt;Object store created.&lt;/li&gt;';
+ };</pre>
+
+<h2 id="Specification">Specification</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('IndexedDB', '#widl-IDBDatabase-createObjectStore-IDBObjectStore-DOMString-name-IDBObjectStoreParameters-optionalParameters', 'createObjectStore()')}}</td>
+ <td>{{Spec2('IndexedDB')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName("IndexedDB 2", "#dom-idbdatabase-createobjectstore", "createObjectStore()")}}</td>
+ <td>{{Spec2("IndexedDB 2")}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
+
+<div>
+
+
+<p>{{Compat("api.IDBDatabase.createObjectStore")}}</p>
+</div>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
+ <li>Starting transactions: {{domxref("IDBDatabase")}}</li>
+ <li>Using transactions: {{domxref("IDBTransaction")}}</li>
+ <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li>
+ <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li>
+ <li>Using cursors: {{domxref("IDBCursor")}}</li>
+ <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
+</ul>