将json数据存储在sqlite(phonegap应用程序)中
store json data in sqlite(phonegap app)
如何将解码后的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");
});
});
代码的问题是:
您尝试插入具有包含数组的payload
的e
。您应该在数组和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);
});
}
});
- 在phonegap应用程序内部重定向不起作用
- 如何将tesseract OCR与Cordova/Phonegap应用程序集成
- 将文件从应用程序文件夹复制到JQM/Phonegap应用程序中的根文件夹
- 录音机未将控制权返回到phonegap应用程序
- 无法在iOS模拟器上运行简单的基于KineticJs的Cordova/PhoneGap应用程序
- Phonegap应用程序检查是否启用了推送通知
- 如何在phonegap应用程序中处理Ajax json响应
- 将json数据存储在sqlite(phonegap应用程序)中
- 将admob广告添加到phonegap应用程序
- jquery保存数据,直到phonegap应用程序关闭
- 如何在启动时在PhoneGap应用程序中调用一些Objective C代码
- Phonegap应用程序SQLite数据库初始设置
- Phonegap应用程序数据库版本在更新后保留
- 用javascript(Phonegap应用程序)加密文件
- 如何为PhoneGap应用程序提供两种不同的UX/UI
- 是否将PDF文件保存在其他应用程序可以访问的PhoneGap应用程序中?(iOS)
- 在phonegap应用程序中打开软键盘时是否会触发Javascript事件
- 如何在PhoneGap应用程序中使用SQLite数据库
- jquery移动phonegap应用程序的语音到文本api
- 将HTML5 PhoneGap应用程序连接到在线数据库