2014-11-24 23 views
-1

我們試圖JSON數據插入到SqlLit。我們需要JSON數據插入到SqlLit。所以我們首先箱表,然後試圖插入JSON VALU表,但它不是work.So請告訴我什麼錯在我的代碼如何將JSON數據到sqlite的DB中的PhoneGap

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中。

+0

解析,並將它推爲對象,而retriving使用字符串化 – 2014-11-24 12:10:31

+0

@MohammedImranN請你當JSON數據插入到數據庫SqlLit這 – 2014-11-24 12:12:32

回答

0

從我看到的也許錯誤是在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 
    }); 
}); 
+0

感謝答覆我們面臨的問題闡述所以請幫我給我一個關於我的問題的小例子 – 2014-11-25 05:12:08

+0

我認爲這是一個錯誤定義id列這樣的「ID INTEGER PRIMARY KEY AUTOINCREMENT」,因爲autoincrement修改默認行爲。由於你在你的代碼中設置了id,所以沒有AUTOINCREMENT。 https://www.sqlite.org/autoinc.html – LuigiDB 2014-11-25 07:21:10

相關問題