sqlite3_step失败:NOT NULL约束失败

时间:2017-04-03 06:55:10

标签: android sqlite ionic-framework

我是离子1的新手。我已经在浏览器中测试了我的应用程序,它运行正常。我用过sqlite。我有一张表格。当我将一个文本输入字段留空时,数据被添加到数据库表中。这正是我想要的。但是,它在设备中不起作用。每当我将一个字段留空时,它会显示以下错误:

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');
                    } 

以下是我创建表格的代码:

$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS items (id integer primary key AUTOINCREMENT,itemname text ,category text ,soldby text ,price REAL ,quantity REAL ,sku integer ,barcode text,timestamp DATE DEFAULT (datetime('now','localtime')),cashier text,total REAL,receitnumber integer,grandtotal REAL)");

1 个答案:

答案 0 :(得分:4)

如果已经存在具有相同名称的表,则

CREATE TABLE IF NOT EXISTS不会更改任何内容,即使它具有不同的结构。

为确保您始终摆脱旧表,请使用:

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

这也会删除所有旧数据。如果要更新表定义但保留旧数据,则必须use a temporary table to do the conversion

相关问题