1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
---
title: IDBFactory.open
slug: Web/API/IDBFactory/open
translation_of: Web/API/IDBFactory/open
---
<p>{{APIRef("IDBFactory")}}</p>
<div class="summary">
<p><strong><code>IDBFactory.open</code></strong> 方法用于<a href="/en-US/docs/IndexedDB#gloss_database_connection">打开一个数据库连接</a>。本方法立即返回一个 {{domxref("IDBOpenDBRequest")}} 对象,但打开数据库的操作是异步执行的。</p>
</div>
<p>连接数据库在一个单独的线程中进行,包括以下几个步骤:</p>
<ol>
<li>指定数据库已经存在时:
<ul>
<li>等待 {{domxref("versionchange")}} 操作完成。</li>
<li>如果数据库已计划删除,那等着删除完成。</li>
</ul>
</li>
<li>如果已有数据库版本高于给定的 <code>version,中止操作并返回</code>类型为 <code style="font-size: 14px;">VersionError 的</code> <code>DOMError</code> 。</li>
<li>如果已有数据库版本低于给定的 <code>version,触发一个</code> <code>versionchange</code> 操作。</li>
<li>如果数据库不存在,创建指定名称的数据库,将版本号设置为给定版本,如果给定版本号,则设置为1,and no object stores.</li>
<li>创建数据库连接。</li>
</ol>
<p>如果操作成功执行,将触发 <code>success</code> 事件 on the request object that is returned from this method, with its <code>result</code> attribute set to the new {{domxref("IDBDatabase")}} object for the connection.</p>
<p>If an error occurs while the database connection is being opened, then an <a href="/en-US/docs/IndexedDB/IDBErrorEvent">error event</a> is fired on the request object returned from this method.</p>
<h2 id="Syntax">Syntax</h2>
<p>For the current standard:</p>
<pre class="syntaxbox"><span class="idlInterface" id="idl-def-IDBFactory"><span class="idlMethod"> <span class="idlMethType"><a class="idlType" href="https://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#idl-def-IDBOpenDBRequest"><code>IDBOpenDBRequest</code></a></span> <span class="idlMethName"><a href="https://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#widl-IDBFactory-open-IDBOpenDBRequest-DOMString-name-unsigned-long-long-version">open</a></span> (<span class="idlParam"><span class="idlParamType">DOMString</span> <span class="idlParamName">name</span></span>, <span class="idlParam">[<span class="extAttr">EnforceRange</span>] optional <span class="idlParamType">unsigned long long</span> <span class="idlParamName">version</span></span>);</span></span></pre>
<p>For the experimental version with <code>options</code> (see below):</p>
<pre class="syntaxbox"><span class="idlInterface"><span class="idlMethod">IDBOpenDBRequest open (DOMString name, optional IDBOpenDBOptions options);</span></span></pre>
<h2 id="示例">示例</h2>
<p>For the current standard:</p>
<pre class="brush: js">var request = window.indexedDB.open("toDoList", 4);</pre>
<p>For the experimental version with <code>options</code> (see below):</p>
<pre class="brush:js;"><code>var request = window.indexedDB.open("toDoList", {version: 4, storage: "temporary"});</code></pre>
<h2 id="参数">参数</h2>
<dl>
<dt>
name</dt>
<dd>
数据库名称</dd>
<dt>
version</dt>
<dd>
指定数据库版本,当你想要更改数据库格式(比如增加对象存储,非增加记录),必须指定更高版本,通过 <span style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;">versionchange 来更改</span></dd>
<dt>
options (version and storage) {{ NonStandardBadge() }}</dt>
<dd>
In Gecko, since <a href="/en-US/Firefox/Releases/26">version 26</a>, you can include an <code>options</code> object as a parameter of {{ domxref("IDBFactory.open") }} that contains the <code>version</code> number of the database, plus a storage value that specifies whether you want to use <code>permanent</code> (the default value) storage for the IndexedDB, or <code>temporary</code> storage (aka shared pool.) See {{ bug("785884") }} for more details. This is a non-standard feature that we are looking to standardise sometime in the future.</dd>
</dl>
<div class="note">
<p><strong>Note</strong>: Data in temporary storage persists until the global limit for the pool is reached. The global limit calculation is relatively complex, but we are considering changing it (see {{ Bug("968272") }}). When the global limit is reached, then data for the least recently used origin is deleted. There's also a group limit (eTLD+1 group/domain) which is currently 20% of the global limit. All requets that would exceed the group limit are just rejected.</p>
</div>
<h2 id="返回">返回</h2>
<dl>
<dt>
{{domxref("IDBOpenDBRequest")}}</dt>
<dd>
The request object on which subsequent events related to this request are fired.</dd>
</dl>
<h2 id="Exceptions">Exceptions</h2>
<p>This method may raise a {{domxref("DOMException")}} with a <a href="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Exception</th>
<th scope="col">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>TypeError</code></td>
<td>The value of version is zero or a negative number or not a number.</td>
</tr>
</tbody>
</table>
<h2 id="Specifications">Specifications</h2>
<p>{{page("/en-US/docs/Web/API/IDBFactory","Specifications")}}</p>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{page("/en-US/docs/Web/API/IDBFactory","Browser_compatibility")}}</p>
|