Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery query.value(0).toString()) is Empty
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery query.value(0).toString()) is Empty

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 499 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kolxz2
    wrote on last edited by kolxz2
    #1

    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 becausequery.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");
        }
    }
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      kolxz2
      wrote on last edited by
      #2

      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");
          }
      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved