Other Languages - How to connect to local database using Phone Gap

Asked By balaji m on 01-Oct-11 05:51 AM
hi frds

how to connect to local database using Phone Gap ?
Is web services work in Phone Gap if Possible how can i acheieve this?
How to communicate with local databases using javascript

Pls help
Reena Jain replied to balaji m on 01-Oct-11 06:05 AM

You need to build a web service and call it using a XMLHttpRequest. Basically, you call a file on your server what does the sql query then returns the result.
The webservice methods can then be called from your PhoneGap project (I would recommend having a look at the jquery ajax api http://api.jquery.com/jQuery.ajax/).

You can also look at Local Storage and use of SQLite database.

try this and let me know
Anoop S replied to balaji m on 01-Oct-11 06:47 AM
You cannot connect to the DB directly, you will need a webserver between your app and the data.

Typically you will use some sort of REST/json api that you implement on your  server, and you will use
XHR to send and receive requests.

Have a look here for more info on XHR:
dipa ahuja replied to balaji m on 01-Oct-11 07:47 AM
var mydb=false;
   // initialise the database
   initDB = function() {
   try {
     if (!window.openDatabase) {
     alert('not supported');
     } else {
     var shortName = 'phonegap';
     var version = '1.0';
     var displayName = 'PhoneGap Test Database';
     var maxSize = 65536; // in bytes
     mydb = openDatabase(shortName, version, displayName, maxSize);
   } catch(e) {
     // Error handling code goes here.
     if (e == INVALID_STATE_ERR) {
     // Version number mismatch.
     alert("Invalid database version.");
     } else {
     alert("Unknown error "+e+".");
Chintan Vaghela replied to balaji m on 01-Oct-11 07:59 AM

Database (the Sqlite database) is baked right into Webkit (and Safari), though you do get some added functionality from Phonegap (the ability to pre-populate a Database). But you can just develop in HTML5 and Javascript to start. Apple provides a reference for Client Side storage. If you are looking for some sample code to play with, here is a little testbed I came up with to play with HTML5 databases. Note: when I say HTML5 databases, I mean Webkit as Mozilla is skeptical about adding an SQL database to HTML5 at all.

Thank YOu