将json数据存储在sqlite(phonegap应用程序)中

store json data in sqlite(phonegap app)

本文关键字:phonegap 应用程序 sqlite json 数据 存储      更新时间:2023-09-26

如何将解码后的JSON数据存储到SQLite数据库中。该应用程序是使用phonegap构建的。

下面是对json数据进行解码的函数。

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;
    }
}

在这里,JSON消息被解码并存储到变量j中。它正在正确显示。我想把它存储到SQLite数据库中。我尝试添加插件,但它显示错误,如"这没有响应"。如何将消息存储到SQL数据库中。

以下是我尝试显示j时得到的内容。

消息->消息:未定义

消息->解码的消息:{"有效载荷":{"0":{"medicine_name":null},"1":{,"11":{"tm_1":null},"12":{,"18":{"tm_3":null},"19":{"dose":null},"20":{"medicine_name":null{,"21":}"tm_1":null,"22":{:"tm_2":null,"23":{'tm_3":null}dose":null},"30":{"medicine_name":null},"31":{,"36":{"指令":null}},"

请帮忙。

这就是我试图将它存储到SQL中的方法。但它不起作用

$("#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");
      });
});

代码的问题是:

您尝试插入具有包含数组payloade。您应该在数组和for上迭代,每条消息都插入它。

此外,你有(?, ?),应该是(?, ?, ?, ?, ?, ?, ?)

注意:

1) 得到MSG: undefined是因为显示了来自e.payload.message的消息,若仔细观察并没有属性message,应该是e.payload

$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + JSON.stringify(e.payload) + '</li>');

2) 您不需要使用JSON.stringify,因为tx.executeSql希望您插入的数据是array(例如[ 'medicine_value', 'tm_1_value1', ... ]

3) 此表不包含id列(向表中添加一个id unique

4) 如果您想查看是否插入了数据,请选择

$("#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 (id unique, medicine_name text,tm_1 integer,tm_2 integer,tm_3 integer,dosage text,diagnosis text,instructions text)');

    var totalItems = 5;
    var currentArray = [];
    var arr = [];
    arr.push(currentArray);
    currentArray.push(arr.length);
    for (var key in e.payload) {
        var prop = e.payload[key]; 
        for (var name in prop) {
            currentArray.push(prop[name]);
            if(i%totalItems == 0) {
                i = 0;
                currentArray.push(null);
                currentArray.push(null);
                currentArray = [];
                arr.push(currentArray);
                currentArray.push(arr.length);
            }
        }   
        i++;
    };
    arr.pop();
    for(var idx = 0; idx < arr.length; idx++)
    {  
        var j=arr[idx];
        tx.executeSql("INSERT OR REPLACE INTO Medicine (id,medicine_name,tm_1,tm_2,tm_3,dosage,diagnosis,instructions) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", j, function(tx, result)
         {
             console.log("Insert succeed");
             console.log(result);
         });
    }
});