Android sqlite3_步骤失败:非空约束失败

Android sqlite3_步骤失败:非空约束失败,android,sqlite,ionic-framework,Android,Sqlite,Ionic Framework,我对爱奥尼亚一号还不熟悉。我已经在浏览器中测试了我的应用程序,效果很好。我使用了sqlite。我有一张表格。当我将一个文本输入字段留空时,数据被添加到数据库表中。这正是我想要的。但是,它在设备中不起作用。每当我将一个字段留空时,它会显示以下错误: Object {message: "sqlite3_step failure: NOT NULL constraint failed: items.barcode", code: 6} 以下是我的代码片段: if ($scope.item.itemn

我对爱奥尼亚一号还不熟悉。我已经在浏览器中测试了我的应用程序,效果很好。我使用了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)");

CREATE TABLE IF NOT EXISTS
如果已经存在同名的表,即使该表具有不同的结构,也不会更改任何内容

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

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

这也会删除所有旧数据。如果您想更新表定义但保留旧数据,则必须。

您是否有过在
条形码
列上具有NOTNULL约束的表的早期版本?是的,我有。然后该怎么办@CL.还有别的办法吗??因为我的应用程序需要保留旧数据。如果我每次更改表结构时都运行drop table,然后删除“drop table”会怎么样?另外,我想知道为什么我的应用程序在浏览器上运行良好,而在我的设备或模拟器上运行不好。该链接显示了如何更改表。当存在旧数据库时,应用程序不工作。@SoumyaRauth“为什么我的应用程序在浏览器上运行良好”完全猜测:浏览器没有数据库的“上一个实例”(以及旧的not NULL约束),因此创建了一个新表(没有该约束)。该设备的前一个实例确实包含(不需要的)约束。