open
Summary
The open method is used to open an IndexedDB database.
Method of apis/indexeddb/IDBFactoryapis/indexeddb/IDBFactory
Syntax
var dbOpenRequest = indexeddb.open(name, version);
Parameters
name
- Data-type
- String
The name of the database to be opened.
version
- Data-type
- Number
(Optional)
The version (unsigned long long) for the database
Return Value
Returns an object of type DOM NodeDOM Node
An IDBOpenRequest request object that fires events to indicate the result of the request.
Examples
Open a database without a version number
var dbOpenRequest = window.indexedDB.open("DatabaseName");
dbOpenRequest.onsuccess = function(event){
var db = dbOpenRequest.result; // This is the database object
};
dbOpenRequest.onerror = function(e){
// Called when an error occurs
};
dbOpenRequest.onblocked = function(e){
// Called if another transaction is open on the database
};
Open a database with a version number that is higher than the existing version number
// The database is already opened with version 1.
var dbOpenRequest = window.indexedDB.open("DatabaseName", 2);
dbOpenRequest.onsuccess = function(event){
var db = dbOpenRequest.result; // This is the database object on which various operations can be performed
};
dbOpenRequest.onupgradeneeded = function(e){
//This occurs when the version specified in the open call is higher that the version of the database. This is the version change transaction.
};
dbOpenRequest.onerror = function(e){
// Called when an error occurs
};
dbOpenRequest.onblocked = function(e){
// Called if another transaction is open on the database
};
Usage
var openRequest = window.indexedDB.open("databaseName", 1);
Notes
The open method either creates a database if it does not exist, or opens one, with the specified version number. If no version number is specified, the database is opened with the current version number. If a database is to be created and a version number is not specified, the database is opened with a version 1.
Related specifications
- W3C IndexedDB Specification
- W3C Proposed Recommendation
Attributions
Microsoft Developer Network: [Windows Internet Explorer API reference Article]