android - sqlite3_step失败: NOT NULL constraint failed

标签 android sqlite ionic-framework

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

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

这是我的代码 fragment :

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 ( [...] );

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

关于android - sqlite3_step失败: NOT NULL constraint failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43178292/

相关文章:

java - 奇怪的崩溃。很少的代码,了解堆栈跟踪

java - 我在使用 libgdx 首选项时遇到问题。不保存高分?

sqlite - 如何使 PRAGMA 选项对于 sqlite3 永久有效

python - 将 pandas DataFrame 转储到 SQL 语句

javascript - AngularJS 无法注入(inject)服务

ios - 输入类型 ="number"不适用于 iOS 设备中的 Ionic 应用程序

当我在 Android Studio 中运行时,Android 应用程序未安装在模拟器或设备上

java - 如何从谷歌blobstore下载文件到android

android - 如何从对话框中删除保存在 sqlite 数据库中的 recyclerview 项目?

android - 如何使用 ionic-cli 使用 Crosswalk lite 构建 android?