我是 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/