Getting Started with HTML5 INDEXEDDB database (ii)

Source: Internet
Author: User

The previous study notes on Indexeddb mainly wrote the basic operation of INDEXEDDB database-adding and deleting; but why should we use INDEXEDDB? Why has indexeddb been favored by developers? The main thing is the core features of INDEXEDDB: With cursors and indexes and scopes to really play INDEXEDDB power

1. Index: Why is it called INDEXEDDB? Because it gives us the index to query the value we need;

2. Cursors: Cursors provide us with the data that we iterate over,

3, the scope: and the cursor together specify the range we need to loop,

4, index, cursor, scope of comprehensive application can bring us a good data interaction experience

To create an index:

1request.onupgradeneeded =function(event) {2db =Event.target.result;3     if(!db.objectstorenames.contains ("table")) {4         varstore = Db.createobjectstore ("table", {keypath: "ssn"});5     }6     varindex = Store.createindex ("Nameindex", "name", {unique:true});//create name index, follow we can find by name7Console.log ("Createobjectstore success!");8};

To read data through a cursor:

1Db.transaction ("table"). ObjectStore ("table"). OpenCursor (). onsuccess =function(Event) {//opencursor method2     vargetString = "";3     varcursor =Event.target.result;4     if(cursor) {5GetString + = "Name for SSN" + Cursor.key + "is" +Cursor.value.name;6Console.log ("Name for SSN" + Cursor.key + "is" +cursor.value.name);7Cursor.Continue();8         //alert (getString);9     }Tendocument.getElementById ("Get_ssn"). InnerHTML + = getstring+ "<br/>"; One}

Find/Read data by index://If an index is created when the data table is created or not accessed through the index

1 db.transaction ("table")2 . ObjectStore ("table")3 . Index ("Nameindex" )4 . Get ("Chenjian")5function() {6     Console.log (this. result.email); 7 };

Mixed application of index and cursors cursor:

1Db.transaction ("Table")2. ObjectStore ("table")3. Index ("Nameindex")4 . OpenCursor (Idbkeyrange.only ("Chenjian"))5. onsuccess =function() {6     varcursor =Event.target.result;7     if(cursor) {8Console.log (Cursor.key +cursor.value.name);9Cursor.Continue();Ten     } One};

The OpenCursor () method can pass in the specified range of parameters:

Specify the range:

1 index.opencursor () the/index.openkeycursor () method takes all the records of the object store when the parameter is not passed, as in the example above we can filter the search 2 You can use the key range to limit the range of values in a cursor, passing it as the first argument to OpenCursor () or openkeycursor () 3 idbkeyrange.only (value): Gets only the specified data 4 Idbkeyrange.lowerbound (Value,isopen): Gets the data with the smallest value, and the second parameter indicates whether to exclude the value itself, that is, whether it is an open interval in mathematics 5 Idbkeyrange.upperbound (Value,isopen): Similar to the above, the data used to get the maximum value is value 6 Idbkeyrange.bound (VALUE1,VALUE2,ISOPEN1,ISOPEN2): no need to explain.

When a specified range is passed in, the cursor will only loop through the range

Getting Started with HTML5 INDEXEDDB database (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.