-2

I created a web api using C# and also created a mobile application using cordova. I am retrieving all data on cordova app using JSON.

Now I want to save this JSON data to client side. Please suggest me the best option to save all the data to client side.

  • SQLLite
  • websql
  • JSON file directly saved to client side or other option.
beaver
  • 17,333
  • 2
  • 40
  • 66
  • Possible duplicate of [What is more efficient? Static, data Passing, shared preferences, Database...?](http://stackoverflow.com/questions/9529302/what-is-more-efficient-static-data-passing-shared-preferences-database) – Much Overflow Feb 14 '16 at 05:54

2 Answers2

0

You can use pouchdb no SQL database here, also you can use cordova sqlite plugin here.

Omar Hassan
  • 727
  • 1
  • 11
  • 24
0

If you need to keep your client-side and server-side DBs in sync, PouchDB is a good choice - you can use pouchdb-phonegap-cordova to implement it in the app. You'd also need implement a CouchDB REST interface on the server-side.

Or since your data is already formatted as JSON, you may want to try cordova-sqlite-porter as a convenient way to store the data - it will interface with either the WebView's WebSQL DB or native SQLite in conjunction with Cordova-sqlite-storage plugin.

You can pass your data as a JSON structure to importJsonToDb() which makes optimisations to perform the operation as fast as possible. This is good if you have a large amount of data to insert. Example usage:

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var json = {
    "structure":{
        "tables":{
            "Artist":"([Id] PRIMARY KEY, [Title])"
        },
       "otherSQL": [
            "CREATE UNIQUE INDEX Artist_ID ON Artist(Id)"
       ]
    },
    "data":{
        "inserts":{
            "Artist":[
                {"Id":"1","Title":"Fred"},
                {"Id":"2","Title":"Bob"},
                {"Id":"3","Title":"Jack"},
                {"Id":"4","Title":"John"}
            ]
        }
    }
};
var successFn = function(count){
    alert("Successfully imported JSON to DB; equivalent to "+count+" SQL statements");
};
var errorFn = function(error){
    alert("The following error occurred: "+error.message);
};
var progressFn = function(current, total){
    console.log("Imported "+current+"/"+total+" statements";
};
cordova.plugins.sqlitePorter.importJsonToDb(db, json, {
    successFn: successFn,
    errorFn: errorFn,
    progressFn: progressFn,
    batchInsertSize: 500
});
DaveAlden
  • 30,083
  • 11
  • 93
  • 155