我是 ionic 框架的新手。我一直在用 SQLite 开发一个应用程序。该应用程序在浏览器上运行良好。但是当我在设备上运行时它说
Cannot read property 'SUM(total)' of undefined
这是我的代码 fragment :
$scope.items = [];
$scope.grandTotal = 0;
$scope.receiptnumber = 0;
$scope.receiptnumber = $scope.receiptnumber + 1;
$scope.items = [];
$scope.item = {};
$scope.grandTotal = null;
$ionicPlatform.ready(function () {
//EXPERIMENT CODE STARTS
var query2 = "SELECT * FROM items WHERE quantity!='' ";
console.log(query2);
$cordovaSQLite.execute(db, query2, []).then(function (res) {
if (res.rows.length > 0) {
for (var i = 0; i < res.rows.length; i++) {
$scope.items.push({
itemname: res.rows.item(i).itemname,
price: res.rows.item(i).price,
quantity: res.rows.item(i).quantity,
});
// $scope.items = $scope.items;
// console.log($scope.items);
$scope.items = $scope.items;
}
} else {
console.log("No results found");
}
}, function (err) {
console.error("error=>" + err);
});
//EXP CODE ENDS
$ionicHistory.clearCache();
$ionicHistory.clearHistory();
//GRAND TOTAL CALCULATION STARTS
var query = "SELECT SUM(total) FROM items";
console.log(query);
$cordovaSQLite.execute(db, query, []).then(function (res) {
// $scope.grandTotal = res.rows[0]['SUM(total)'];
console.log("SUM TOTAL TEST");
console.log(res.rows);
}, function (err) {
console.error("error=>" + err);
});
//GRAND TOTAL CALCULATION ENDS
//Back button STARTS
$scope.myGoBack = function () {
$state.go("menu.sales");
// window.history.back();
};
//BackButton ends
//Charge button functionality STARTS===============================/////////////////////////
$scope.charge = function () {
$state.go('transactionsuccess');
// INITIAL UPDATE OF ORDER NUMBER STARTS
// var query5 = "UPDATE receipt SET ordernumber=" + "'" + $scope.receiptnumber + "'" + " WHERE quantity!='' OR quantity!='undefined' ";
// console.log(query5);
// $cordovaSQLite.execute(db, query5, []).then(function (result) {
// $scope.items = [];
// $scope.grandTotal = 0;
// }, function (err) {
// console.error(err);
// });
//ENDS
}
});
我所做的不是将 SUM 值保存到数据库中,而是直接从程序中获取它。这是由这段代码完成的:
$scope.grandTotal = res.rows[0]['SUM(total)'];
因为我不知道有什么替代方法可以获取 SUM(total) 并将其保存在变量中,以便它在浏览器和设备上都能正常工作...谢谢。
最佳答案
The name of a result column is the value of the "AS" clause for that column, if there is an AS clause. If there is no AS clause then the name of the column is unspecified and may change from one release of SQLite to the next.
因此您必须使用 SELECT sum(total) AS MyLittleSum ...
来获得可预测的列名。
关于android - SQLIte 的“SUM(total)”在 Android 设备上不起作用,但在浏览器上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974552/