{"id":8183,"date":"2012-09-24T14:59:46","date_gmt":"2012-09-24T09:29:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8183"},"modified":"2012-10-28T13:42:28","modified_gmt":"2012-10-28T08:12:28","slug":"creating-database-in-phonegap","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-database-in-phonegap\/","title":{"rendered":"Creating database in Phonegap"},"content":{"rendered":"<p>Currently I am building a mobile app using Phonegap. In my project I need to store the data into the database and refresh it with server time to time.<\/p>\n<p>Phonegap have the Storage API to do this. Storage API is based on the W3C HTML5 webdatabase. So we just need to create the database and corresponding table according to the webdatabase specification e.g;<br \/>\n[java]<br \/>\nvar db = openDatabase(&quot;myApp&quot;, 1.0, &quot;App database&quot;,200000)<br \/>\n[\/java]<br \/>\nNow to create tables and insert data into it using this db instance<br \/>\n[java]<br \/>\ndb.transaction(function(transaction){<br \/>\n    transaction.executeSql(&quot;CREATE TABLE user (username Integer default NULL, UNIQUE(username));&quot;);<br \/>\n    transaction.executeSql(&quot;INSERT INTO user (username) values (&#8216;uday&#8217;);&quot;)<br \/>\n})<br \/>\n[\/java]<br \/>\nNow to get the data from this table we will simply write the select query. The executeSql method has its own success and failure callback method. Success callback gives the result set from the query e.g;<br \/>\n[java]<br \/>\ndb.transaction(function(transaction){<br \/>\n    transaction.executeSql(&quot;SELECT * from user where username=?&quot;,[&#8216;uday&#8217;],successCallback,function(e){<br \/>\n       console.debug(&quot;some error&quot;)<br \/>\n   })<br \/>\n})<\/p>\n<p>function successCallback(transaction,results){<br \/>\n    for (var i = 0; i &lt; results.rows.length; i++) {<br \/>\n        var username = results.rows.item(i).username;<br \/>\n       \/\/ Some code<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<br \/>\nThe executeSql method takes four argument, first is the query, second array of values, if needed in the query, third success callback, which returns the result of query and last one error callback.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently I am building a mobile app using Phonegap. In my project I need to store the data into the database and refresh it with server time to time. Phonegap have the Storage API to do this. Storage API is based on the W3C HTML5 webdatabase. So we just need to create the database and [&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":[1043,1044,921,1042],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8183"}],"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=8183"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8183\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}