【问题标题】:how to insert json Data into Sqlite DB in PhoneGap如何在PhoneGap中将json数据插入Sqlite DB
【发布时间】: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


【解决方案1】:

据我所知,错误可能在 json 处理程序中。

尝试使用类似的东西:

$.support.cors=true;                        // this two allow you to ask page outside ypur phone
$.mobile.allowCrossDomainPages = true;      //
$.getJSON('http://headers.jsontest.com/?callback=?',function(data){
    $.each(data, function(i, dat){
        $("#someul").append('<li>'+dat.rep+'</li>');  //i send data in json using rep as index
    });
});

【讨论】:

  • 感谢您的回复,当 json 数据插入 SqlLit 数据库时,我们遇到了问题,所以请帮我举个关于我的问题的小例子
  • 我认为定义像这样的“id INTEGER PRIMARY KEY AUTOINCREMENT”这样的id列是一个错误,因为自动增量修改了默认行为。由于您在代码中设置了 id 尝试不使用 AUTOINCREMENT。 sqlite.org/autoinc.html
猜你喜欢
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 2014-04-28
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多