Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Problema con QSqlQuery e size
Forum Update on Monday, May 27th 2025

Problema con QSqlQuery e size

Scheduled Pinned Locked Moved Solved Italian
6 Posts 3 Posters 1.4k Views
  • 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.
  • F Offline
    F Offline
    fermatqt
    wrote on last edited by
    #1

    ciao!

    ho la necessità di contare il numero di records di una query, in quanto devo eseguire una determinata operazione nel caso fosse zero.
    ho provato così:

                QSqlQuery query;
                query.prepare(strQuery);
                query.bindValue(0, conto);
                query.exec();
                qDebug() << query.size();
    

    ma restituisce sempre -1, anche nel caso di record trovati.
    come posso fare??

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Che tipo di query e'? Se e' un SELECT to basta mettere un if(!query.next()) qDebug("0 risultati"); dopo query.exec(); (che dovrebbe essere anch'esso in un if, comunque)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • F Offline
        F Offline
        fermatqt
        wrote on last edited by
        #3

        ciao!
        è una semplice SELECT.

        ho fatto così:

                    if (!query.next()) {
                        qDebug() << "KO";
                    } else {
                        while (query.next()) {
                            ui->baseTable->insertRow(ui->baseTable->rowCount());
                            for (int i = 0; i < header.size(); ++i) {
                                ui->baseTable->setItem(rows, i, new QTableWidgetItem(query.value(header.at(i)).toString()));
                            }
                            rows++;
                        }
                    }
        

        però succede che se non trova record esce il KO (quindi corretto).
        se li trova, non li visualizza nel while.
        nello specifico sembra ne visualizzi uno in meno.

        JonBJ 1 Reply Last reply
        0
        • F fermatqt

          ciao!
          è una semplice SELECT.

          ho fatto così:

                      if (!query.next()) {
                          qDebug() << "KO";
                      } else {
                          while (query.next()) {
                              ui->baseTable->insertRow(ui->baseTable->rowCount());
                              for (int i = 0; i < header.size(); ++i) {
                                  ui->baseTable->setItem(rows, i, new QTableWidgetItem(query.value(header.at(i)).toString()));
                              }
                              rows++;
                          }
                      }
          

          però succede che se non trova record esce il KO (quindi corretto).
          se li trova, non li visualizza nel while.
          nello specifico sembra ne visualizzi uno in meno.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @fermatqt
          If someone wishes to translate this into Italian... :)

          1. http://doc.qt.io/qt-5/qsqlquery.html#size:

          Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.

          Even if your query is good, you may not get information about size/number of rows. Does whatever SQL back-end you are using support returning this?

                      if (!query.next()) {
                          qDebug() << "KO";
                      } else {
                          while (query.next()) {
          

          Are you aware that this code throws away the first row in the if (!query.next()) ? The while (query.next()) only sees & counts row #2 onward.

          Ciao!

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5
            bool haAlmenoUnaRiga = false;
            for(;query.next();haAlmenoUnaRiga=true) {
                ui->baseTable->insertRow(ui->baseTable->rowCount());
                for (int i = 0; i < header.size(); ++i) {
                    QTableWidgetItem* tempItem = new QTableWidgetItem;
                    tempItem->setData(Qt::EditRole,query.value(header.at(i)));
                    ui->baseTable->setItem(rows, i, tempItem);
                }
                rows++;
            }
            if(!haAlmenoUnaRiga)
            qDebug() << "KO";
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            F 1 Reply Last reply
            0
            • VRoninV VRonin
              bool haAlmenoUnaRiga = false;
              for(;query.next();haAlmenoUnaRiga=true) {
                  ui->baseTable->insertRow(ui->baseTable->rowCount());
                  for (int i = 0; i < header.size(); ++i) {
                      QTableWidgetItem* tempItem = new QTableWidgetItem;
                      tempItem->setData(Qt::EditRole,query.value(header.at(i)));
                      ui->baseTable->setItem(rows, i, tempItem);
                  }
                  rows++;
              }
              if(!haAlmenoUnaRiga)
              qDebug() << "KO";
              
              F Offline
              F Offline
              fermatqt
              wrote on last edited by
              #6

              @VRonin said in Problema con QSqlQuery e size:

              bool haAlmenoUnaRiga = false;
              for(;query.next();haAlmenoUnaRiga=true) {
                  ui->baseTable->insertRow(ui->baseTable->rowCount());
                  for (int i = 0; i < header.size(); ++i) {
                      QTableWidgetItem* tempItem = new QTableWidgetItem;
                      tempItem->setData(Qt::EditRole,query.value(header.at(i)));
                      ui->baseTable->setItem(rows, i, tempItem);
                  }
                  rows++;
              }
              if(!haAlmenoUnaRiga)
              qDebug() << "KO";
              

              sembra funzionare alla perfezione.
              grazie mille!!

              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