{"id":6953,"date":"2012-09-03T17:10:51","date_gmt":"2012-09-03T11:40:51","guid":{"rendered":"http:\/\/www.tothenew.com\/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:\/\/www.tothenew.com\/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=\"http:\/\/www.tothenew.com\/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":1},"categories":[1],"tags":[922,924,925,921,923],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6953"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6953"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6953\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}