IDBFactory.open - Web API 接口参考 编辑

IDBFactory.open 方法用于打开一个数据库连接。本方法立即返回一个 IDBOpenDBRequest 对象,但打开数据库的操作是异步执行的。

连接数据库在一个单独的线程中进行,包括以下几个步骤:

  1. 指定数据库已经存在时:
    • 等待 versionchange 操作完成。
    • 如果数据库已计划删除,那等着删除完成。
  2. 如果已有数据库版本高于给定的 version,中止操作并返回类型为 VersionError 的 DOMError 。
  3. 如果已有数据库版本低于给定的 version,触发一个 versionchange 操作。
  4. 如果数据库不存在,创建指定名称的数据库,将版本号设置为给定版本,如果给定版本号,则设置为1,and no object stores.
  5. 创建数据库连接。

如果操作成功执行,将触发 success 事件 on the request object that is returned from this method, with its result attribute set to the new IDBDatabase object for the connection.

If an error occurs while the database connection is being opened, then an error event is fired on the request object returned from this method.

Syntax

For the current standard:

 IDBOpenDBRequest open (DOMString name, [EnforceRange] optional unsigned long long version);

For the experimental version with options (see below):

IDBOpenDBRequest open (DOMString name, optional IDBOpenDBOptions options);

示例

For the current standard:

var request = window.indexedDB.open("toDoList", 4);

For the experimental version with options (see below):

var request = window.indexedDB.open("toDoList", {version: 4, storage: "temporary"});

参数

name
数据库名称
version
指定数据库版本,当你想要更改数据库格式(比如增加对象存储,非增加记录),必须指定更高版本,通过 versionchange 来更改
options (version and storage) 非标准
In Gecko, since version 26, you can include an options object as a parameter of IDBFactory.open that contains the version number of the database, plus a storage value that specifies whether you want to use permanent (the default value) storage for the IndexedDB, or temporary 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.

Note: 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.

返回

IDBOpenDBRequest
The request object on which subsequent events related to this request are fired.

Exceptions

This method may raise a DOMException with a DOMError of the following types:

Exception描述
TypeErrorThe value of version is zero or a negative number or not a number.

Specifications

SpecificationStatusComment
Indexed Database API
The definition of 'IDBFactory' in that specification.
Recommendation 
Indexed Database API 2.0
The definition of 'IDBFactory' in that specification.
Recommendation 

浏览器兼容性

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. Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 24
Full support 24
No support 23 — 57Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support YesFirefox Full support 16
Full support 16
No support 10 — 16Prefixed
Prefixed Implemented with the vendor prefix: moz
IE Full support 10Notes
Full support 10Notes
Notes partial
Opera Full support 15Safari Full support 7WebView Android Full support Yes
Full support Yes
No support ? — 57Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android Full support 25
Full support 25
No support 25 — 57Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Mobile ?Firefox Android Full support 22Opera Android Full support 22Safari iOS Full support 8Samsung Internet Android Full support Yes
Full support Yes
No support ? — 7.0Prefixed
Prefixed Implemented with the vendor prefix: webkit
Available in workersChrome Full support YesEdge Full support YesFirefox Full support 37IE ?Opera Full support YesSafari ?WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 37Opera Android Full support YesSafari iOS ?Samsung Internet Android Full support Yes
cmpChrome Full support 24
Full support 24
No support 23 — 24Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support 12Firefox Full support 16
Full support 16
No support 10 — 16Prefixed
Prefixed Implemented with the vendor prefix: moz
IE Full support 10Notes
Full support 10Notes
Notes partial
Opera Full support 15Safari Full support 7WebView Android Full support Yes
Full support Yes
No support ? — ?Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android Full support 25Edge Mobile Full support YesFirefox Android Full support 22Opera Android Full support 22Safari iOS Full support 8Samsung Internet Android Full support Yes
deleteDatabaseChrome Full support 24
Full support 24
No support 23 — 24Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support 12Firefox Full support 16
Full support 16
No support 10 — 16Prefixed
Prefixed Implemented with the vendor prefix: moz
IE Full support 10Notes
Full support 10Notes
Notes partial
Opera Full support 15Safari Full support 7WebView Android Full support Yes
Full support Yes
No support ? — ?Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android Full support 25Edge Mobile Full support YesFirefox Android Full support 22Opera Android Full support 22Safari iOS Full support 8Samsung Internet Android Full support Yes
openChrome Full support 24
Full support 24
No support 23 — 24Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support 12Firefox Full support 16
Full support 16
No support 10 — 16Prefixed
Prefixed Implemented with the vendor prefix: moz
IE Full support 10Notes
Full support 10Notes
Notes partial
Opera Full support 15Safari Full support 7WebView Android Full support Yes
Full support Yes
No support ? — ?Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android Full support 25Edge Mobile Full support YesFirefox Android Full support 22Opera Android Full support 22Safari iOS Full support 8Samsung Internet Android Full support Yes

Legend

Full support
Full support
Compatibility unknown
Compatibility unknown
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:128 次

字数:33690

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文