QSqlQuery query.value(0).toString()) is Empty
Solved
General and Desktop
-
I want to change the Inst value in the database. To do this, the user enters how much to increase the value. Then I take the data from the table, increase it and overwrite it. But I do not increase the number, but only replaced by the one that the user entered. That's because
query.value(0).toInt()
contains nothing. Where did the error occur? The SQL request is correct.Please help me figure it out.
void GiveGet::on_pushButton_clicked() { int id, copy; QSqlQuery query; id = ui->spinBox->value(); query.prepare("select Inst from list_book WHERE id=?;"); query.addBindValue(id); if (!query.exec()) { QMessageBox::critical(this, tr("error::"), query.lastError().text()); } else { QMessageBox::warning(this, tr("Copyed"),query.value(0).toString()); } copy = ui->spinBox_2->value() + query.value(0).toInt(); query.prepare("UPDATE list_book " "SET " "Inst = ?" " WHERE id=?"); query.addBindValue(copy); query.addBindValue(id); if (!query.exec()) { QMessageBox::critical(this, tr("error::"), query.lastError().text()); } else { QMessageBox::warning(this, tr("Saved"),"OK"); } }
-
additionally if your query returns more than one row, you should iterate via query.next()
void GiveGet::on_pushButton_clicked() { int id, copy; QSqlQuery query; id = ui->spinBox->value(); query.prepare("select Inst from list_book WHERE id=?;"); query.addBindValue(id); while(query.next()) { copy = ui->spinBox_2->value() + query.value(0).toInt(); } query.prepare("UPDATE list_book " "SET " "Inst = ?" " WHERE id=?"); query.addBindValue(copy); query.addBindValue(id); if (!query.exec()) { QMessageBox::critical(this, tr("error::"), query.lastError().text()); } else { QMessageBox::warning(this, tr("Saved"),"OK"); } }