Skip to content

Instantly share code, notes, and snippets.

@glynrob
Created February 24, 2013 21:03
Show Gist options
  • Save glynrob/5025607 to your computer and use it in GitHub Desktop.
Save glynrob/5025607 to your computer and use it in GitHub Desktop.
Indexed DB API connect
function openDb() {
var req = indexedDB.open(DB_NAME, DB_VERSION);
req.onsuccess = function (evt) { // success connection to the database so display rows
db = this.result;
displayRows();
};
req.onerror = function (evt) { // error - display error
displayActionFailure("DB error: "+ evt.target.errorCode);
};
req.onupgradeneeded = function (evt) { // upgrade version has changed so upgrade
var store = evt.currentTarget.result.createObjectStore(
DB_TABLE_NAME, { keyPath: 'id', autoIncrement: true });
store.createIndex('title', 'title', { unique: false }); // create fields
//store.createIndex('contents', 'contents', { unique: false }); // any other data you wish to add to the list
};
}
function getObjectStore(storename, mode) {
var tx = db.transaction(storename, mode);
return tx.objectStore(storename);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment