0

我的计算有问题。在构建系统并对其进行测试时,界面中显示的正确数量未显示/保存在数据库中。金额与界面中的金额不同。

笔记:

  1. SELECT SUM(TotalPrice) FROM Sold (Sold 表的屏幕截图将附在一起。
  2. 我将附上下面的屏幕截图。看看它以清楚地了解情况。
  3. 我已经用注释标记了问题代码//<--THIS-->//

这是我的代码:

void CashRegister::on_pushButton_next_clicked()
{
    ui->tableView->show();
    ui->pushButton_finish->show();
    ui->label_totalAmount->show();

    QString receiptNo = ui->label_receiptNum->text();
    QString itemSelect = ui->comboBox_itemID->currentText();
    QString qtySold = ui->lineEdit_qtySold->text();
    QDate currentDate = QDate::currentDate();   //<--Solved-->//
    //QString totalPrice1 = ui->label_totalAmount->text();

    double price=0, totalPrice=0;

    QSqlQuery myqry;
    myqry.exec("SELECT ItemPrice, ItemName FROM Item WHERE ItemId='"+itemSelect+"' ");
    if (myqry.next())
        price = myqry.value(0).toDouble();

    totalPrice = price * qtySold.toInt();

    QString totalPriceString = QString::number(totalPrice);

    myqry.prepare("INSERT INTO Sold (ReceiptId, ItemId, SoldQuantity, TotalPrice) VALUES ('"+receiptNo+"', '"+itemSelect+"', '"+qtySold+"', '"+totalPriceString+"') ");
    myqry.exec();

    ui->comboBox_itemID->setCurrentIndex(0);
    ui->lineEdit_qtySold->clear();

    myqry.prepare("SELECT Item.ItemId, Item.ItemName, Item.ItemPrice, Sold.SoldQuantity FROM Item, Sold WHERE Item.ItemId = Sold.ItemId AND Sold.ReceiptId='"+receiptNo+"' ");
    myqry.exec();


    QSqlQueryModel *myModel = new QSqlQueryModel();
    myModel->setQuery(myqry);
    ui->tableView->setModel(myModel);

    double sum = 0;

    myqry.exec("SELECT SUM(TotalPrice) FROM Sold WHERE ReceiptId = '"+receiptNo+"' ");
    if (myqry.next())
        sum += myqry.value(0).toDouble();

    ui->label_totalAmount->setText("TOTAL AMOUNT: RM" + QString::number(sum));

    QString totalPriceReceipt = QString::number(sum);

    //<--THIS-->//
    myqry.prepare("INSERT INTO Receipt (ReceiptId, ReceiptDate, ReceiptAmount) VALUES ('"+receiptNo+"', '"+currentDate.toString()+"', '"+totalPriceReceipt+"') ");
    myqry.exec();
}

接口和数据库中的不同数量

售出表

4

0 回答 0