1

I am having trouble deciding hot to implement a database for my mobile app. I am using javascript with jquery mobile and phonegap to hopefully deploy to IOS and Android. The database is basically a list of about 60-70 location names, description, latitude and longitude. I need the data to be available even if the user does not have internet access and need to perform queries such as sorting the locations by closest distance.

Is there a way to create the database file beforehand and open as needed or do I need to create the database each time when my app opens? Does the database file reside on the device even after the app is closed or does it create it again when app is restarted?

Any suggestions or examples?

Thanks,

Robert

BGecko
  • 241
  • 2
  • 6
  • 15

3 Answers3

2

There are several types of browser storage such as localStorage they are all built in and can be used directly.

Storage objects are a recent addition to the standard. As such they may not be present in all browsers.........The maximum size of data that can be saved is severely restricted by the use of cookies.

Code sample:

  function storeMyContact(id) {
    var fullname    = document.getElementById('fullname').innerHTML;
    var phone       = document.getElementById('phone').innerHTML;
    var email       = document.getElementById('email').innerHTML;
    localStorage.setItem('mcFull',fullname);
    localStorage.setItem('mcPhone',phone);
    localStorage.setItem('mcEmail',email);
  }

On the other hand, localStorage might not be enough, therefore, external libraries come to hand which actually utilize the browsers built in storage and make the db works cross browsers.

1- SQL like DB sequelsphere (looks like suitable for heavy lifting!)

Code sample for query that will run directly from the browser:

SELECT empl_id, name, age 
  FROM empl 
 WHERE age < 30 

2- JSON like DB taffydb (looks like suitable for every day activity!)

// Create DB and fill it with records
var friends = TAFFY([
    {"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active"},
    {"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active"},
    {"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active"},
    {"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active"}   
]);

   // Find all the friends in Seattle
   friends({city:"Seattle, WA"});

3- Since you mentioned mobile, then jstorage is a cross-browser key-value store database to store data locally in the browser - jStorage supports all major browsers, both in desktop (yes - even Internet Explorer 6) and in mobile.

If you would like to have more options ->(client-side-browser-database)

Community
  • 1
  • 1
Mohammed Joraid
  • 6,202
  • 2
  • 27
  • 38
0

The easiest would be to use localStorage.

window.localStorage.setItem("key", "value");
var value = window.localStorage.getItem("key");

If you need to store more data and have complex queries use a real Database.

Both can be found in the Cordova Docs on Storage

kraftner
  • 467
  • 2
  • 19
0

There is also pouch db. I use it with my Ionic App. Works great and very simple to learn and use. I use local storage only for minor temporarily used data (within a session). To persist data even when app is closed and re-opened, Pouchdb works great. By default, it's calls are async. Works well with Promises.

Amrudesh
  • 305
  • 4
  • 6