dimanche 22 mars 2015

store json data in sqlite(phonegap app)

How can I store the JSON data that is decoded into SQLite Database. The app is built using phonegap.


Here is the function in which the json data is getting decoded.



function onNotification(e) {
switch( e.event )
{
case 'message':
$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
var j=JSON.stringify(e);
$("#app-status-ul").append('<li>MESSAGE -> MSG DECODED: ' + j + '</li>');
//android only
$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
//amazon-fireos only
$("#app-status-ul").append('<li>MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp + '</li>');
break;
}
}


Here the JSON message is decoded and stored to a variable j. It is getting displayed properly. I want to store that into SQLite database. I tried by adding the plugin but it is showing error like "This is not responding". How can I store the message into SQL database.


Here is what I get when I try to display j.


MESSAGE -> MSG: undefined MESSAGE -> MSG DECODED: {"payload":{"0":{"medicine_name":null},"1":{"tm_1":null},"2":{"tm_2":null},"3":{"tm_3":null},"4":{"dosage":null},"5":{"medicine_name":null},"6":{"tm_1":null},"7":{"tm_2":null},"8":{"tm_3":null},"9":{"dosage":null},"10":{"medicine_name":null},"11":{"tm_1":null},"12":{"tm_2":null},"13":{"tm_3":null},"14":{"dosage":null},"15":{"medicine_name":null},"16":{"tm_1":null},"17":{"tm_2":null},"18":{"tm_3":null},"19":{"dosage":null},"20":{"medicine_name":null},"21":{"tm_1":null},"22":{"tm_2":null},"23":{"tm_3":null},"24":{"dosage":null},"25":{"medicine_name":null},"26":{"tm_1":null},"27":{"tm_2":null},"28":{"tm_3":null},"29":{"dosage":null},"30":{"medicine_name":null},"31":{"tm_1":null},"32":{"tm_2":null},"33":{"tm_3":null},"34":{"dosage":null},"35":{"diagnosis":null},"36":{"instructions":null}},"


Please help.


This is what i tried to do for storing it into SQL. But it didnt work



$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
var db = window.sqlitePlugin.openDatabase({name: "drrem.db", location: 1});
db.transaction(function(tx)
{
tx.executeSql('DROP TABLE IF EXISTS Medicine');
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (medicine_name text,tm_1 integer,tm_2 integer,tm_3 integer,dosage text,diagnosis text,instructions text)');
var j=[hash, JSON.stringify(e, null, "")];
tx.executeSql("INSERT OR REPLACE INTO Medicine (medicine_name,tm_1,tm_2,tm_3,dosage,diagnosis,instructions) VALUES (?, ?)", j, function(tx, result)
{
console.log("insertId: " + result.insertId + " -- probably 1");
console.log("rowsAffected: " + result.rowsAffected + " -- should be 1");
});
});

Aucun commentaire:

Enregistrer un commentaire