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 Updated to NodeBB v4.3 + New Features

Problema con QSqlQuery e size

Scheduled Pinned Locked Moved Solved Italian
6 Posts 3 Posters 1.6k 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.
  • F Offline
    F Offline
    fermatqt
    wrote on 13 Jul 2018, 12:19 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
    • V Offline
      V Offline
      VRonin
      wrote on 13 Jul 2018, 14:42 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 13 Jul 2018, 15:07 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.

        J 1 Reply Last reply 13 Jul 2018, 15:27
        0
        • F fermatqt
          13 Jul 2018, 15:07

          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.

          J Offline
          J Offline
          JonB
          wrote on 13 Jul 2018, 15:27 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
          • V Offline
            V Offline
            VRonin
            wrote on 16 Jul 2018, 08:02 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 17 Jul 2018, 09:47
            0
            • V VRonin
              16 Jul 2018, 08:02
              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 17 Jul 2018, 09:47 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

              1/6

              13 Jul 2018, 12:19

              • Login

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