2017-05-30 96 views
0

嗨,我試圖動態ORDER BY我的sqlite的角度。但它總是說錯誤rcould不準備聲明..它重定向到我的訂單字符串..但是,當我手動設置我的ASC或DESC它工作得很好。我的問題是如何將我的字符串設置爲我的查詢SQLError無法準備語句

這是我的js。該order_by_field等於名稱和順序等於ASC或DESC

//Sort Priority Ascending 
      $scope.sortAsc = function() { 
       sort_orderby('name', 'asc'); 
       $scope.orderByPopover.hide(); 
      }; 

      //Sort Priority Descending 
      $scope.sortDesc = function() { 
       sort_orderby('name', 'desc'); 
       $scope.orderByPopover.hide(); 
      }; 

      function sort_orderby(order_by_field, order) { 

       var query = "SELECT * FROM listJobs ORDER BY '" + order_by_field + "' '"+ order +"' "; 
       $cordovaSQLite.execute(db, query, []) 
        //If success 
        .then(function(data) { 

          offlineGetJobList(); 
          console.log(data.rows) 
         }, 
         function(err) { 
          console.error(err); 
         }); 



      } 
+0

請了'做了'console.log'查詢「併發布結果。 – 31piy

+1

嘗試從查詢中刪除單引號。 – Jayvee

+0

@ 31piy這是我的結果SELECT * FROM listJobs ORDER BY'priority''asc'..但是當我遵循jayvee的建議,它的作品魅力:D ..謝謝jayvee。當我添加單引號時,我想它會變成字符串 – VLR

回答

1

var query = "SELECT * FROM listJobs ORDER BY '" + order_by_field + "' '"+ order +"' "; 

應該

var query = "SELECT * FROM listJobs ORDER BY " + order_by_field + " "+ order +" ";