【发布时间】:2014-11-24 10:13:26
【问题描述】:
我们尝试将JSON 数据插入SqlLit。我们需要JSON 数据插入SqlLit。所以首先我们创建表,然后尝试在表中插入JSON 值,但它不起作用。所以请告诉我我的代码有什么问题
function startup() {
alert("1");
console.log("Starting up...");
db = window.openDatabase("Database", "1.0", "Cordova Demo",10485760);
db.transaction(function(tx){
alert("2");
tx.executeSql('DROP TABLE IF EXISTS HEADER_DATA');
tx.executeSql("create table if not exists docs(id INTEGER PRIMARY KEY AUTOINCREMENT, language TEXT, cookie TEXT, host TEXT, control TEXT)");
},function(tx,error){
alert("3");
console.log('tx error: ' + error);
},function(){
alert("4");
console.log('tx success');
insert();
});
}
对于插入:--
function insert(){
alert("5");
var output = $('#output').text('Refreshing documentation...');
//$("#docs").html("Refreshing documentation...");
$.ajax({
url: 'http://headers.jsontest.com/',
type: 'GET',
contentType: "application/json;charset=utf-8",
success: function (data) {
alert("6");
// iterate over data and save it to DB
output.empty();
$.each(data, function(i,item){
alert("7");
tx.executeSql('INSERT INTO docs (id, language, cookie, host ,control) VALUES(' + item.id + ', "' + item.Accept-Language + '", "' + item.Cookie + '", "' + item.Host + '", "' + item.Cache-Control+'")');
});
output.append(landmark);
});
},
error: function (x, y, z) {
alert(x.responseText);
}
});
}
my json :--
{
"Accept-Language": "en-US,en;q=0.8",
"Cookie": "__hbblk_headersjsontestcom=0",
"Host": "headers.jsontest.com",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36",
"Via": "1.1 FFTMG",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Cache-Control": "max-age=0"
}
在我的代码表中创建成功,但是当尝试将数据插入表时它不起作用。所以请给我任何将 JSON 数据插入 SqlLit DB 的小例子。
【问题讨论】:
-
解析并将其作为对象推送,并在检索时使用 stringify
-
@MohammedImranN 你能详细说明一下吗
标签: jquery json sqlite jquery-mobile cordova