Common database operations

This topic describes how to accomplish some common database operations with IndexedDB. It contains the following sections:

Adding objects to an object store

To add objects to an object store, open the object store and call the add method.

Updating a record object in an object store

To update a record object in an object store, use the put method and set the key value of the record you want to replace. Be aware that this adds a new record if the key value does not already exist in the object store.

Updating a record object in a cursor

To update a record object in a cursor, use the update method of the cursor. Be aware that this will create a new record if the original record has been deleted previously.

Accessing individual records in an object store

To access individual records in an object store:

  • To return an individual record in an object store, open the object store and then use the get method to return the object that corresponds to the key value.
  • To retrieve the set of records in an object store, open the object store and then use the openCursor method to obtain a cursor that lets you iterate through each record.

Filtering the records in an object store

Use a key range to filter the records in an object store. This returns a cursor that you can iterate. If you want this cursor to present records in a different order, such as descending order or sorted by a common attribute value, pass the key range as a parameter to the openCursor method of an index. You can also:

  • Define a key range, open the object store, and then use the key range as a parameter to the openCursor method of the object store.
  • Open the object store, define a new or open an existing index for the object store, and then call the openCursor method on the index. Be aware that you can also pass a key range as an optional parameter to the openCursor method.

Deleting records from an object store or cursor

Use the delete method to remove individual records from an object store or a cursor. Use the clear method to delete all records from an object store.

Related topics

IE Test Drive: Cookbook demo
Asynchronous versus synchronous
Indexed Database API ("IndexedDB")
Internet Explorer 10 Guide for Developers

 

 

Show:
© 2014 Microsoft. All rights reserved.