2017-04-03 123 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

链接显示如何更改表格。存在旧数据库时,该应用程序不起作用。 –

相关问题