QSqlQuery :: record :: value :: toInt()产生意外结果

时间:2018-04-20 10:13:17

标签: sqlite qt5

我正在使用Qt5和sqlite3制作图书馆管理软件。我定义了issueBook函数来向用户发行一本书。这是issueBook的代码:

void MainWindow :: onIsssueDetailsEntered(const QString &name, const QDate &date)
{

QSqlQuery query;

if(!query.exec("SELECT QUANTITY FROM allBooks WHERE UID = " + uid + ";") )
        QMessageBox::information(this, "Oops!", "Selection failed: " + query.lastError().text());
else
{
    int val = query.record().value(0).toInt();

    if(val <= 0)
        QMessageBox::critical(this, "Oops!", "You have no book in stock to issue");
    else
    {
        query.exec("UPDATE allBooks SET QUANTITY = " + QString :: number(val - 1) + " WHERE UID = " + uid + ";");

        query.prepare("INSERT INTO log (STUDENT, UID, ISSUED) VALUES (?, ?, ?)");

        query.addBindValue(name);
        query.addBindValue(uid);
        query.addBindValue(date.toString(Qt::RFC2822Date));

        if(!query.exec())
        {
            QMessageBox::critical(this, "Oops!", query.lastError().text());
        }
    }
}
}

第5行从终端执行时显示输出1.但是val的值为0.因此,程序显示错误消息“您没有库存库存”。我不知道为什么会这样。

1 个答案:

答案 0 :(得分:0)

替换Sub ProjectUpdate() Call ProjectName Call ProjectFunding Call ProjectFundingFormat MsgBox "Done" End Sub
int val = query.record().value().toInt()

它有效