aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbfactory/index.html
blob: 57d23ec08c7f17c4dffb8b56b55cb12bc2f9773e (plain)
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
80
81
82
83
84
85
86
87
88
89
---
title: IDBFactory
slug: Web/API/IDBFactory
translation_of: Web/API/IDBFactory
---
<p>{{APIRef("IndexedDB")}}</p>

<div>
<p><a href="/en-US/docs/IndexedDB">IndexedDB API</a> 的<code>IDBFactory</code> 接口让程序可以异步存取 indexed databases。<code>window.indexedDB</code> 对象实现了这个接口。你可以通过这个对象而不是直接使用<code>IDBFactory</code>接口打开—— 创建或者连接 —— 和删除一个数据库。</p>
</div>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("IDBFactory.open")}}</dt>
 <dd>请求打开一个数据库的连接(<a href="https://developer.mozilla.org/en-US/docs/IndexedDB#gloss_database_connection">connection to a database</a>)。</dd>
 <dt>{{domxref("IDBFactory.deleteDatabase")}}</dt>
 <dd>请求删除数据库。</dd>
 <dt>{{domxref("IDBFactory.cmp")}}</dt>
 <dd>比较两个键的方法并返回一个结果,表明哪个值更大。</dd>
 <dt>
 <h3 id="过时的Methods">过时的Methods</h3>
 </dt>
 <dt><code><a href="https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory.open-obsolete">IDBFactory.open</a></code>, the original version {{ obsolete_inline }}</dt>
 <dd>一个被废弃的方法请求打开一个数据库的连接,仍然在一些浏览器中被实施(<a href="https://developer.mozilla.org/en-US/docs/IndexedDB#gloss_database_connection">connection to a database</a>).</dd>
</dl>

<h2 id="Example">Example</h2>

<p>In the following code snippet, we make a request to open a database, and include handlers for the success and error cases. For a full working example, see our <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> app (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</p>

<pre class="brush:js;highlight:[10]">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 += '&lt;li&gt;Error loading database.&lt;/li&gt;';
};

DBOpenRequest.onsuccess = function(event) {
  note.innerHTML += '&lt;li&gt;Database initialised.&lt;/li&gt;';

  // store the result of opening the database in the db variable. This is used a lot later on, for opening transactions and suchlike.
  db = request.result;
};
</pre>

<h2 id="Specifications">Specifications</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', '#idl-def-IDBFactory', 'IDBFactory')}}</td>
   <td>{{Spec2('IndexedDB')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>

{{Compat("api.IDBFactory")}}

<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>