{"id":6953,"date":"2012-09-03T17:10:51","date_gmt":"2012-09-03T11:40:51","guid":{"rendered":"https:\/\/2thenew.today\/blog\/?p=6953"},"modified":"2016-12-19T15:06:20","modified_gmt":"2016-12-19T09:36:20","slug":"pre-populating-database-in-phonegap-app","status":"publish","type":"post","link":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/","title":{"rendered":"Pre populating database in phonegap app"},"content":{"rendered":"<p>Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly.<\/p>\n<p>My app required bundling the  database into the app but there is no straight forward way in Phonegap to do this but I found a way to do this.<br \/>\nI added &#8220;app.sql&#8221; file in my &#8220;www&#8221; folder and get the text of this file. My app.sql looks something like this<\/p>\n<p>[java]<br \/>\nCREATE TABLE &#8216;category&#8217; (categoryId long NOT NULL, name varchar(255) NOT NULL);\\n<br \/>\nINSERT INTO &#8216;category&#8217; VALUES(1,&#8217;cat_1&#8242;);\\n<br \/>\nINSERT INTO &#8216;category&#8217; VALUES(2,&#8217;cat_2&#8242;);\\n<br \/>\nINSERT INTO &#8216;category&#8217; VALUES(3,&#8217;cat_3&#8242;);\\n<br \/>\nINSERT INTO &#8216;category&#8217; VALUES(4,&#8217;cat_4&#8242;);\\n<br \/>\n[\/java]<\/p>\n<p>I am using jquery in my example<\/p>\n<p>[java]<br \/>\nfunction runInitialSetup() {<br \/>\n   var filePath = &quot;app.sql&quot;;<br \/>\n   $.get(filePath, function (response) {<br \/>\n         var statements = response.split(&#8216;\\n&#8217;);<\/p>\n<p>         var shortName = &quot;database&quot;;<br \/>\n         var version = &#8216;1.0&#8217;;<br \/>\n         var displayName = &#8216;App Database&#8217;;<br \/>\n         var maxSize = 200000; \/\/  bytes<br \/>\n         db = openDatabase(shortName, version, displayName, maxSize);<\/p>\n<p>         db.transaction(function (transaction) {<br \/>\n            jQuery.each(statements, function (index, value) {<br \/>\n                if (value != &#8221;) {<br \/>\n                    transaction.executeSql(value, [], successHandler, function (e) {<br \/>\n                        console.log(&quot;Error executing sql &quot; + value)<br \/>\n                    });<br \/>\n                }<br \/>\n            });<br \/>\n         });<br \/>\n    });<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>I executed above method whenever the app is installed first time.<\/p>\n<p>&nbsp;<\/p>\n<p>Lets see whats happening in the above example. I read my app.sql file which was bundled in the app and the response I got is the text, so I split the content on newline character. There is the assumption that each sql statement is separated with newline character and the content in sql do not have the newline character.<\/p>\n<p>&nbsp;<\/p>\n<p>Then I made the database connection and executed each statement to setup the database.<\/p>\n<p>&nbsp;<\/p>\n<p>You can read any text file bundled in your app build. You can always read the content from your bundled app but never think of writing files because its not possible.<\/p>\n<p>&nbsp;<\/p>\n<p>Hope it helps<\/p>\n<p><a href=\"https:\/\/2thenew.today\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7,"footnotes":""},"categories":[1],"tags":[922,924,925,921,923],"class_list":["post-6953","post","type-post","status-publish","format-standard","hentry","category-technology","tag-bundle-database-in-phonegap","tag-database-connection-in-phonegap","tag-executing-sql-in-phonegap","tag-phonegap","tag-reading-bundled-file-in-phonegap"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Uday Pratap Singh\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Pre populating database in phonegap app | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Pre populating database in phonegap app | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#article\",\"name\":\"Pre populating database in phonegap app | TO THE NEW Blog\",\"headline\":\"Pre populating database in phonegap app\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2012-09-03T17:10:51+05:30\",\"dateModified\":\"2016-12-19T15:06:20+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#webpage\"},\"articleSection\":\"Technology, bundle database in phonegap, database connection in phonegap, executing sql in phonegap, Phonegap, reading bundled file in phonegap\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#listItem\",\"name\":\"Pre populating database in phonegap app\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#listItem\",\"position\":3,\"name\":\"Pre populating database in phonegap app\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/\",\"name\":\"Uday Pratap Singh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/d4c98503-ff55-4112-8fc2-e3c4fcc6fc3a_282-Uday-Pratap-Singh-PROFILEPICTURE.jpeg\",\"width\":96,\"height\":96,\"caption\":\"Uday Pratap Singh\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/\",\"name\":\"Pre populating database in phonegap app | TO THE NEW Blog\",\"description\":\"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/pre-populating-database-in-phonegap-app\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\"},\"datePublished\":\"2012-09-03T17:10:51+05:30\",\"dateModified\":\"2016-12-19T15:06:20+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Pre populating database in phonegap app | TO THE NEW Blog","description":"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no","canonical_url":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#article","name":"Pre populating database in phonegap app | TO THE NEW Blog","headline":"Pre populating database in phonegap app","author":{"@id":"https:\/\/2thenew.today\/blog\/author\/uday\/#author"},"publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"},"datePublished":"2012-09-03T17:10:51+05:30","dateModified":"2016-12-19T15:06:20+05:30","inLanguage":"en-US","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#webpage"},"articleSection":"Technology, bundle database in phonegap, database connection in phonegap, executing sql in phonegap, Phonegap, reading bundled file in phonegap"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.today\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/2thenew.today\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#listItem","name":"Pre populating database in phonegap app"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#listItem","position":3,"name":"Pre populating database in phonegap app","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.today\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.today\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.today\/blog\/author\/uday\/#author","url":"https:\/\/2thenew.today\/blog\/author\/uday\/","name":"Uday Pratap Singh","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/d4c98503-ff55-4112-8fc2-e3c4fcc6fc3a_282-Uday-Pratap-Singh-PROFILEPICTURE.jpeg","width":96,"height":96,"caption":"Uday Pratap Singh"}},{"@type":"WebPage","@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#webpage","url":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/","name":"Pre populating database in phonegap app | TO THE NEW Blog","description":"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.today\/blog\/author\/uday\/#author"},"creator":{"@id":"https:\/\/2thenew.today\/blog\/author\/uday\/#author"},"datePublished":"2012-09-03T17:10:51+05:30","dateModified":"2016-12-19T15:06:20+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.today\/blog\/#website","url":"https:\/\/2thenew.today\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Pre populating database in phonegap app | TO THE NEW Blog","og:description":"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no","og:url":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/","og:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Pre populating database in phonegap app | TO THE NEW Blog","twitter:description":"Currently I am working on developing mobile app using Phonegap. Phonegap comes up with very nice javascript methods to access phone specific api and the developer like me who have fair knowledge of javascript HTML and CSS can learn it very quickly. My app required bundling the database into the app but there is no","twitter:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"6953","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 13:41:10","updated":"2024-02-29 08:48:11","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tPre populating database in phonegap app\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.today\/blog"},{"label":"Technology","link":"https:\/\/2thenew.today\/blog\/category\/technology\/"},{"label":"Pre populating database in phonegap app","link":"https:\/\/2thenew.today\/blog\/pre-populating-database-in-phonegap-app\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/6953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/comments?post=6953"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/6953\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/media?parent=6953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/categories?post=6953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/tags?post=6953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}