2017-03-03 69 views
1

我有一個textfield deviceid我試圖將該值存儲到sqlite數據庫,然後試圖以其他形式檢索它,但我得到了下面的錯誤。這是工作正常,當我在桌面上運行它,但我得到了錯誤,當我嘗試在phonegap應用程序中運行。Phonegap Sqlte:語句回調引發異常或聲明errorcallback沒有返回false

錯誤處理SQL:聲明回調引發異常或聲明errorcallback沒有返回false New.html:

<script type="text/javascript">                                                                      document.addEventListener("deviceready", onDeviceReady, false); 
      var db,did; 
      function onDeviceReady() { 
       db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024); 
       db.transaction(createDB, errorCB, successCB); 
      } 
      function createDB(tx) { 
       alert("createDB"); 
       tx.executeSql('DROP TABLE IF EXISTS USERIDDETAILS'); 
       tx.executeSql('CREATE TABLE IF NOT EXISTS USERIDDETAILS (deviceid TEXT NOT NULL)'); 
      } 
      function errorCB(err) { 
       alert("Error processing SQL: "+err.code); 
       console.log("Error processing SQL: "+err.message); 
      } 
      function successCB() { 
       alert("success"); 
       console.log("success"); 
      } 
      function insertDB(tx) { 
       did = $("[name='deviceid']").val(); 
       var sql='INSERT INTO USERIDDETAILS(deviceid)VALUES(?)'; 
       tx.executeSql(sql,[did],sucessQueryDB,errorCB); 
       console.log(did); 
       alert(did); 
      } 
      function sucessQueryDB(tx){ 
       alert("selected"); 
       tx.executeSql('SELECT * FROM USERIDDETAILS',[],renderList,errorCB); 
      } 
      function renderList(tx,results){ 
      alert("rendering"); 
       var htmlstring=''; 
       var len=results.rows.length; 
       alert("rendered called"+ len); 
       for(var i=0;i<len;i++){ 
        htmlstring += '<li>'+ results.rows.item(i).deviceid+'</li>'; 
       } 
       $('#resultList').html(htmlstring); 
       $('#resultList').listview('refresh'); 
      } 

      function onClick(){ 
       db.transaction(insertDB,errorCB); 
       window.open("new1.html"); 
      return false; 
     }</script> 
</head> 
<body> 
    <div id="header"> 
     <h3> Login </h3> 
    </div> 
    <div id="section"> 
     <form id="myForm"> 
      <br><input type="text" class="one" name="deviceid" id="deviceid" placeholder="DeviceId"><br> 
      <input type="button" class="button" value="SUBMIT" onclick="return onClick()"> 
     </form> 

    </div> 

New1.html:

script type="text/javascript"> 
     var db; 
     function loaddb() 
     { 
      db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024); 
      db.transaction(successCB,errorCB); 
      console.log("loaded"); 
      alert("loaded"); 
     } 
     function errorCB(err) { 
      alert("Error processing SQL: "+err.code); 
      alert("Error processing SQL: "+err.message); 
      console.log("Error processing SQL: "+err.message); 
     } 
     function successCB() { 
      console.log("success"); 
      alert("success"); 
     } 

     function queryDB(tx){ 
      tx.executeSql('SELECT * FROM USERIDDETAILS',[],renderList1,errorCB);  
      console.log("selected"); 
      alert("selected"); 
     } 
     function repopulateId(){ 
      db.transaction(queryDB,errorCB); 
      console.log("populated"); 
      alert("populated"); 
     } 
     function renderList1(tx,results){ 
      console.log("rendering"); 
      alert("rendering"); 
      var htmlstring=''; 
      var len=results.rows.length; 
      var did = results.rows.item(0).deviceid; 
      $('#deviceid').val(did); 
     }</head> 

<body> 
    <div id="header"> 
     <h3> User Profile </h3> 
    </div> 
    <div> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
     loaddb(); 
     repopulateId(); 
     }); 
    </script> 
    </div> 
    <div id="section"> 
     <form method="post" id="myForm"> 
      Device Id:<br><input type="text" class="one" name="deviceid" id="deviceid"> 
      <ul data-role="listview" id="resultList"> 
      </ul> 
     </form> 
    </div> 
</body> 

燦任何人都可以幫助我解決問題。Thankyou。

回答

0

由於sqlite調用是異步的,因此執行不會等待查詢返回的數據。

$(document).ready(function() { 
     loaddb(); 
     //repopulateId(); 
     }); 

呼叫repopulateId()LOADDB()功能的成功回調。

function successCB() { 
      console.log("success"); 
      alert("success"); 
      repopulateId(); 
     } 

希望這將有助於

+0

Ⅰ號我得到了同樣的錯誤,如果我在successCB又稱它爲...其實我忘了,當我在桌面上運行此它工作正常提,當我我試圖在手機phonegap應用程序中我收到此錯誤@Naresh庫馬爾 – Anusha

+0

@Anusha,可能你需要在new1.html中使用deviceready事件也 –

+0

我試過使用deviceready事件也再次我得到同樣的錯誤。@ Naresh庫馬爾 – Anusha