2017-04-03 226 views
0

我是離子1.我已經在瀏覽器中測試了我的應用程序,它工作正常。我用過sqlite。我有一個表格。當我將一個文本輸入字段留空時,數據被添加到數據庫表中。這正是我想要的。但是,它不適用於設備。每當我離開一個字段爲空它顯示了以下錯誤:sqlite3_step失敗:NOT NULL約束失敗

Object {message: "sqlite3_step failure: NOT NULL constraint failed: items.barcode", code: 6} 

這裏是我的代碼片段:

if ($scope.item.itemname && $scope.item.soldby && $scope.item.price) { 
         var query = "INSERT into items (itemname,category,soldby,price,sku,barcode) VALUES(?,?,?,?,?,?)"; 

         $cordovaSQLite.execute(db, query, [$scope.item.itemname, $scope.item.category, $scope.item.soldby, $scope.item.price, $scope.item.sku, $scope.item.barcode,]) 
          .then(function (result) { 
           console.log($scope.item.itemname); 
           console.log($scope.item.category); 
           console.log($scope.item.soldby); 
           console.log($scope.item.price); 
           console.log($scope.item.sku); 
           console.log("Printing barcode"); 
           console.log($scope.item.barcode); 
           console.log($scope.item.total); 

           //ITEM ADDED SUCCESS POPUP STARTS     




           //ITEM ADDED SUCCESS POPUP ENDS 


          }, function (error) { 
           console.log(error); 

          }); 

         // $scope.item = { 
         //  itemname: $scope.item.itemname, 
         // }; 
         $state.go('menu.allItems'); 
        } 

這裏是我創建表的代碼:

​​
+1

您是否曾在「條形碼」列上使用NOT NULL約束表的以前版本? –

+0

是的,我確實有。 –

+0

那怎麼辦? @CL。 –

回答

1

CREATE TABLE IF NOT EXISTS如果同名表已經存在,即使它具有不同的結構,也不會改變任何內容。

爲了確保您始終擺脫舊錶,使用:

DROP TABLE IF EXISTS items; 
CREATE TABLE items ([...]); 

這也刪除所有舊數據。如果您想更新表格定義但保留舊數據,則必須use a temporary table to do the conversion

+0

有沒有其他方法?因爲我的應用需要保留舊數據。如果我在表結構中進行更改時運行drop table,然後刪除「drop table」,該怎麼辦? –

+0

另外,我想知道爲什麼我的應用程序可以在瀏覽器上正常工作,但不是我的設備或模擬器。 –

+0

鏈接顯示如何更改表格。存在舊數據庫時,該應用程序不起作用。 –