{"id":875,"date":"2016-03-17T16:11:00","date_gmt":"2016-03-17T16:11:00","guid":{"rendered":"https:\/\/ionic.io\/blog\/?p=875"},"modified":"2017-01-09T07:03:26","modified_gmt":"2017-01-09T07:03:26","slug":"screencast-ionic-and-sqlite","status":"publish","type":"post","link":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite","title":{"rendered":"Screencast: Ionic and SQLite"},"content":{"rendered":"<p>Ahh, SQLite, the plugin that everyone wants. The SQLite plugin for Cordova is a very popular plugin that allows devs to bypass browser-based storage by creating a full SQLite DB. But the plugin can be intimidating, especially for those who&#8217;ve never dealt with database queries before. Thankfully, we can use <a href=\"https:\/\/pouchdb.com\/\">PouchDB<\/a> as an abstraction. Check it out!<\/p>\n<div class=\"video-container\">\n              <iframe data-src=\"https:\/\/www.youtube.com\/embed\/1HQEzdssoCQ\"frameborder=\"0\" allowfullscreen src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe>\n            <\/div>\n<p><!--more--><\/p>\n<p>Here&#8217;s some of the code I used to set up PouchDB:<\/p>\n<pre><code>.service(&#039;DBService&#039;, function($q) {\n  var items;\n  var db;\n  var self = this;\n\n  this.initDB = function() {\n    return db = new PouchDB(&#039;simpleDB&#039;, {\n      adapter: &#039;websql&#039;\n    });\n  };\n\n  this.getDB = function() {\n\n    if (!items) {\n      return $q.when(\n          db.allDocs({\n            include_docs: true\n          }))\n        .then(function(docs) {\n          items = docs.rows.map(function(row) {\n            row.doc.Date = new Date(row.doc.Date);\n            return row.doc;\n          });\n\n          \/\/ Listen for changes on the database.\n          db.changes({\n              live: true,\n              since: &#039;now&#039;,\n              include_docs: true\n            })\n            .on(&#039;change&#039;, function(change) {\n              self.onDatabaseChange(change)\n            });\n          return items;\n        });\n    } else {\n      return $q.when();\n    };\n\n  };\n\n  this.onDatabaseChange = function(change) {\n    var index = self.findIndex(items, change.id);\n    var item = items[index];\n\n    items.splice(index, 0, change.doc) \/\/ insert\n  }\n\n  this.findIndex = function(array, id) {\n    var low = 0,\n      high = array.length,\n      mid;\n    while (low &lt; high) {\n      mid = (low + high) &gt;&gt;&gt; 1;\n      array[mid]._id &lt; id ? low = mid + 1 : high = mid\n    }\n    return low;\n  }\n\n  this.storeData = function(data) {\n    return $q.when(db.post({\n      &#039;title&#039;: data\n    }))\n  };\n\n  return this\n})\n<\/code><\/pre>\n<h3>Conclusion<\/h3>\n<p>While you could still use SQLite without PouchDB, the built-in adapter makes the process much easier. You can write to your data store like you would an regular JavaScript object, knowing that it will translate over to a SQL data store. Hopefully, this screencast will simplify the process of using the SQLite plugin in your app! Thanks for watching.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ahh, SQLite, the plugin that everyone wants. The SQLite plugin for Cordova is a very popular plugin that allows devs to bypass browser-based storage by creating a full SQLite DB. But the plugin can be intimidating, especially for those who&#8217;ve never dealt with database queries before. Thankfully, we can use PouchDB as an abstraction. Check [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"publish_to_discourse":"","publish_post_category":"","wpdc_auto_publish_overridden":"","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"","discourse_permalink":"","wpdc_publishing_response":"","wpdc_publishing_error":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[3,12],"class_list":["post-875","post","type-post","status-publish","format-standard","hentry","category-all","tag-ionic","tag-sqlite-plugin"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v23.0 (Yoast SEO v23.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Screencast: Ionic and SQLite - Ionic Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Screencast: Ionic and SQLite\" \/>\n<meta property=\"og:description\" content=\"Ahh, SQLite, the plugin that everyone wants. The SQLite plugin for Cordova is a very popular plugin that allows devs to bypass browser-based storage by creating a full SQLite DB. But the plugin can be intimidating, especially for those who&#8217;ve never dealt with database queries before. Thankfully, we can use PouchDB as an abstraction. Check [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite\" \/>\n<meta property=\"og:site_name\" content=\"Ionic Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-03-17T16:11:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-01-09T07:03:26+00:00\" \/>\n<meta name=\"author\" content=\"Mike Hartington\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@mhartington\" \/>\n<meta name=\"twitter:site\" content=\"@ionicframework\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Hartington\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#article\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite\"},\"author\":{\"name\":\"Mike Hartington\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/c8c92b04d526adb925ea514c619a267b\"},\"headline\":\"Screencast: Ionic and SQLite\",\"datePublished\":\"2016-03-17T16:11:00+00:00\",\"dateModified\":\"2017-01-09T07:03:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite\"},\"wordCount\":142,\"commentCount\":13,\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"keywords\":[\"Ionic\",\"SQLite plugin\"],\"articleSection\":[\"All\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite\",\"url\":\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite\",\"name\":\"Screencast: Ionic and SQLite - Ionic Blog\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/#website\"},\"datePublished\":\"2016-03-17T16:11:00+00:00\",\"dateModified\":\"2017-01-09T07:03:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ionic.io\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Screencast: Ionic and SQLite\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ionic.io\/blog\/#website\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"name\":\"ionic.io\/blog\",\"description\":\"Build amazing native and progressive web apps with the web\",\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ionic.io\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/ionic.io\/blog\/#organization\",\"name\":\"Ionic\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"width\":1920,\"height\":854,\"caption\":\"Ionic\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/ionicframework\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/c8c92b04d526adb925ea514c619a267b\",\"name\":\"Mike Hartington\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/08\/mike-headshot-2-smaller-150x150.png\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/08\/mike-headshot-2-smaller-150x150.png\",\"caption\":\"Mike Hartington\"},\"description\":\"Director of Developer Relations\",\"sameAs\":[\"https:\/\/twitter.com\/mhartington\",\"https:\/\/x.com\/mhartington\"],\"url\":\"https:\/\/ionic.io\/blog\/author\/mike\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Screencast: Ionic and SQLite - Ionic Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite","og_locale":"en_US","og_type":"article","og_title":"Screencast: Ionic and SQLite","og_description":"Ahh, SQLite, the plugin that everyone wants. The SQLite plugin for Cordova is a very popular plugin that allows devs to bypass browser-based storage by creating a full SQLite DB. But the plugin can be intimidating, especially for those who&#8217;ve never dealt with database queries before. Thankfully, we can use PouchDB as an abstraction. Check [&hellip;]","og_url":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite","og_site_name":"Ionic Blog","article_published_time":"2016-03-17T16:11:00+00:00","article_modified_time":"2017-01-09T07:03:26+00:00","author":"Mike Hartington","twitter_card":"summary_large_image","twitter_creator":"@mhartington","twitter_site":"@ionicframework","twitter_misc":{"Written by":"Mike Hartington","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#article","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite"},"author":{"name":"Mike Hartington","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/c8c92b04d526adb925ea514c619a267b"},"headline":"Screencast: Ionic and SQLite","datePublished":"2016-03-17T16:11:00+00:00","dateModified":"2017-01-09T07:03:26+00:00","mainEntityOfPage":{"@id":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite"},"wordCount":142,"commentCount":13,"publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"keywords":["Ionic","SQLite plugin"],"articleSection":["All"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite","url":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite","name":"Screencast: Ionic and SQLite - Ionic Blog","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/#website"},"datePublished":"2016-03-17T16:11:00+00:00","dateModified":"2017-01-09T07:03:26+00:00","breadcrumb":{"@id":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/ionic.io\/blog\/screencast-ionic-and-sqlite#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ionic.io\/blog"},{"@type":"ListItem","position":2,"name":"Screencast: Ionic and SQLite"}]},{"@type":"WebSite","@id":"https:\/\/ionic.io\/blog\/#website","url":"https:\/\/ionic.io\/blog\/","name":"ionic.io\/blog","description":"Build amazing native and progressive web apps with the web","publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ionic.io\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ionic.io\/blog\/#organization","name":"Ionic","url":"https:\/\/ionic.io\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","width":1920,"height":854,"caption":"Ionic"},"image":{"@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/ionicframework"]},{"@type":"Person","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/c8c92b04d526adb925ea514c619a267b","name":"Mike Hartington","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/08\/mike-headshot-2-smaller-150x150.png","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/08\/mike-headshot-2-smaller-150x150.png","caption":"Mike Hartington"},"description":"Director of Developer Relations","sameAs":["https:\/\/twitter.com\/mhartington","https:\/\/x.com\/mhartington"],"url":"https:\/\/ionic.io\/blog\/author\/mike"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/comments?post=875"}],"version-history":[{"count":0,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/875\/revisions"}],"wp:attachment":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/media?parent=875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/categories?post=875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/tags?post=875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}