cordovapersistencelocal-storagephonegap-plugins

How persistent localStorage with Cordova 3.6.x?


I'm interested to know how persistent localStorage is on Cordova 3.6.x?

Is it good enough to store configuration of application?

What happens if I update application from AppStore/GooglePlay is it still hold the collected data of user?

If not what cordova plugin will you suggest me to use if I want a persistent and pre-populated data with cordova application?

Thanks in advance.


Solution

  • I've seen many links regarding local storage, unfortunately definite answer isn't found yet. I can suggest you a database, SQLite wrapper plugin

    This is simple and works just fine. Hopefully it'll fulfill all your requirements including pre-populated database.

    Few examples:

    //Pre-populated database
    
    //For Android & iOS (only): put the database file in the www directory and open the database like:
    
    var db = window.sqlitePlugin.openDatabase({name: "my.db", createFromLocation: 1});
    

    Usage

    // Wait for Cordova to load
    document.addEventListener("deviceready", onDeviceReady, false);
    
    // Cordova is ready
    function onDeviceReady() {
      var db = window.sqlitePlugin.openDatabase({name: "my.db"});
    
      db.transaction(function(tx) {
        tx.executeSql('DROP TABLE IF EXISTS test_table');
        tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
    
        // demonstrate PRAGMA:
        db.executeSql("pragma table_info (test_table);", [], function(res) {
          console.log("PRAGMA res: " + JSON.stringify(res));
        });
    
        tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
          console.log("insertId: " + res.insertId + " -- probably 1");
          console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
    
          db.transaction(function(tx) {
            tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
              console.log("res.rows.length: " + res.rows.length + " -- should be 1");
              console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
            });
          });
    
        }, function(e) {
          console.log("ERROR: " + e.message);
        });
      });
    }
    

    You can know everything from the documentation.