Interface中的计算量不会保存到SQLlite DB中,只保存不同的金额

时间:2018-05-12 15:30:10

标签: c++ qt qt4 qsqlquerymodel

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

注意:

  1. SELECT SUM(TotalPrice)FROM Sold(已售出表格的屏幕截图将附在一起。
  2. 我将附上以下屏幕截图。仔细看看情况。
  3. 我已使用评论 //< - THIS - > // 标记了问题代码。
  4. 这是我的代码:

    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();
    }
    

    Different amount in Interface and DB

    Sold Table

0 个答案:

没有答案