HTML5 client database easy to use: IndexedDB

IndexedDB introduce
IndexedDB is a lasting memory of your browser structured data object database, and provides a rich query capabilities for web applications.
Compared to the Web SQL database it more simple, and standard work on the official Web SQL has stopped.
Compared to Web Storage, IndexedDB storage space is unlimited and permanent.
Creating a database
IndexedDB independent space is assigned by the domain name, a domain name can be created at a plurality of separate databases, each database can create multiple object storage spaces (table / object repository), a plurality of object storage space may store object data (index field).
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
function OpenDB () {
var = indexedDB.open Request (dbName, dbVer); // if the database exists opened, if the database is not present go to New
request.onsuccess = function (e ) {
}
request.onerror = function (E) {
}
// create a new database, the database is changed or when the version number starting onupgradeneeded event and execute the callback function
request.onupgradeneeded = function (e) {

}

}
IndexedDB.open method for creating a database, which pass two parameters (database name, database version), request.onupgradeneeded method is called when creating a new database or database version number changes
created object storage space
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
request.onupgradeneeded = function (E) {
DB = e.target.result;
// determine whether the object exists in the repository
IF (db.objectStoreNames.contains ( 'the Users')!) {
// If the object repository does not exist, creating a new object repository (keyPath, primary key; autoIncrement, whether the increment), returns an object (objectStore)
var = db.createObjectStore Store ( 'the Users', {keyPath: 'ID', autoIncrement: to true} );
// specified field can be indexed, uNIQUE field is unique, multiple indexes can be created
store.createIndex ( 'username', 'username', {uNIQUE: to false})
}
}
Store.createIndex create an index field, inside pass three parameters (index name, index fields, is unique)
transaction (transaction)
IndexedDB for queries and updates are included in a transaction (transaction) in order to ensure that these operations either succeed together or fail together.
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
function dataHandle (Data) {
// create a transaction object
var db.transaction TS = ( 'the Users', 'ReadWrite');
// Get the transaction object by object repository
var store = ts .objectStore ( 'the Users');
// add data to the repository
var store.put REQ = (data);
req.onsuccess = function () {

}

}
Operation of the warehouse store:
PUT () to add data, the parameter for the object to be saved, if the primary key data (the keypath) is already the same change data.
add () to add data, the parameter for the object to be saved, if the primary key data (the keypath) is already the same failure is saved.
delete () delete data, its argument is a primary key target data. get () to obtain data, its argument is a primary key target data.
Through the data
through the data object repository cursor within the search range of a cursor
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
var IDBKeyRange.lowerBound Range = (. 1); // search range specified cursor
var req = store.openCursor (range ); // Create a cursor
dbdata = [];
req.onsuccess = function () {
var cursor = this.result;
IF (cursor) {
dbData.push (cursor.value);
cursor.continue (); // read cycle until the end of the data fetch
} else {

}

}
IDBKeyRange several major methods:
IDBKeyRange.bound (n1, n2, to false, to false); primary key range from n1 to n2, and the third contains four parameter n1 or n2
IDBKeyRange.only (n-); a primary key range
IDBKeyRange.lowerBound (n, false); n is greater than the primary key set
IDBKeyRange.upperBound (n, false); n is less than the primary key set
query data
can be queried data needs to be store.createIndex () created an index
. 1
2
. 3
. 4
var ts.objectStore = Store ( 'the Users');
var index = store.index ( "username"); // open the database index
var range = IDBKeyRange.only (keyName); // pass a single bond (index value) Get the Range
var REQ = index.openCursor (the Range);
delete the database
1
indexedDB.deleteDatabase ( "database name");
summarized
above completes the creation of a database, create an object storage space for data to traverse through the transaction, adding, the operation of the query.

Guess you like

Origin blog.51cto.com/yanhuang/2479932