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

QSqlQuery error handling

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 2.8k 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.
  • jsulmJ jsulm

    @SPlatten Why do you set table and column names in ``?

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #6

    @jsulm , So I edited again and replaced .lastError().number() with .lastError().text().toLatin1().data(). There is no error displayed.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SPlattenS SPlatten

      @jsulm , why not? its good practice and encapsulates the text.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #7

      @SPlatten said in QSqlQuery error handling:

      encapsulates the text

      Which text? We are talking about table and column names.

      What does https://doc.qt.io/qt-5.15/qsqlquery.html#size return after query execution?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      SPlattenS 1 Reply Last reply
      0
      • jsulmJ jsulm

        @SPlatten Why do you set table and column names in ``?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #8

        @jsulm , Is something wrong with this code:

            QSqlRecord record(query.record());
            int intTotal(record.count());
            if ( intTotal == 0 )
            {
                return;
            }
            if ( query.next() )
            {
                QSqlField field(record.field(0));
                QVariant varValue(field.value());
                if ( varValue.isValid() != true )
                {
                    return;
                }
        //Send binary data
                sendBinary(lngBlockNo, varValue.toByteArray());
        

        Kind Regards,
        Sy

        jsulmJ 1 Reply Last reply
        0
        • jsulmJ jsulm

          @SPlatten said in QSqlQuery error handling:

          encapsulates the text

          Which text? We are talking about table and column names.

          What does https://doc.qt.io/qt-5.15/qsqlquery.html#size return after query execution?

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #9

          @jsulm size returns 1. Which is correct, but field shows as "" in the debugger, varValue also shows as "" in debugger. When I get into the function sendBinary there is a check for the byte array:

          if ( crbaData.length() == 0 ) {
              return;
          }
          

          It does return because the array length is 0.

          Kind Regards,
          Sy

          jsulmJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @jsulm , Is something wrong with this code:

                QSqlRecord record(query.record());
                int intTotal(record.count());
                if ( intTotal == 0 )
                {
                    return;
                }
                if ( query.next() )
                {
                    QSqlField field(record.field(0));
                    QVariant varValue(field.value());
                    if ( varValue.isValid() != true )
                    {
                        return;
                    }
            //Send binary data
                    sendBinary(lngBlockNo, varValue.toByteArray());
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @SPlatten said in QSqlQuery error handling:

            QSqlRecord record(query.record());
            int intTotal(record.count());
            if ( intTotal == 0 )
            {
            return;
            }

            Why do you need this?

            while (query.next()) {
            ...
            }
            

            is enough.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            SPlattenS 1 Reply Last reply
            0
            • jsulmJ jsulm

              @SPlatten said in QSqlQuery error handling:

              QSqlRecord record(query.record());
              int intTotal(record.count());
              if ( intTotal == 0 )
              {
              return;
              }

              Why do you need this?

              while (query.next()) {
              ...
              }
              

              is enough.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #11

              @jsulm , ok, I can remove that, but that doesn't explain the problem.

              Kind Regards,
              Sy

              jsulmJ 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @jsulm size returns 1. Which is correct, but field shows as "" in the debugger, varValue also shows as "" in debugger. When I get into the function sendBinary there is a check for the byte array:

                if ( crbaData.length() == 0 ) {
                    return;
                }
                

                It does return because the array length is 0.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @SPlatten Also please read what https://doc.qt.io/qt-5.15/qsqlrecord.html#count actually returns

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                SPlattenS 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @SPlatten Also please read what https://doc.qt.io/qt-5.15/qsqlrecord.html#count actually returns

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #13

                  @jsulm , I've removed that check but still doesn't explain why there is no error and no data, yet the data exists and the query is correct.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @jsulm , ok, I can remove that, but that doesn't explain the problem.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #14

                    @SPlatten Do you get valid data with this:

                    while (query.next()) {
                        QByteArray data = query.value(0).toByteArray();
                        ...
                    }
                    

                    ?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    SPlattenS 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @SPlatten Do you get valid data with this:

                      while (query.next()) {
                          QByteArray data = query.value(0).toByteArray();
                          ...
                      }
                      

                      ?

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #15

                      @jsulm , thank you, I really don't know why but prior to today the code I posted was working, I've just modified it using your example and its ok.

                      Kind Regards,
                      Sy

                      KroMignonK 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @jsulm , thank you, I really don't know why but prior to today the code I posted was working, I've just modified it using your example and its ok.

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #16

                        @SPlatten AFAIK, QSqlQuery::record() is to be used after QSqlQuery::exec() to get the field information of the current query.
                        So you can find the indexes of the value and decode faster the results:

                        if(query.exec("SELECT * from MyTable where status = 2"))
                        {
                             auto record = query.record();
                             int nameCol = record.indexOf("name");
                             while(query.next())
                                 qDebug() << "Found:" << query.value(nameCol ).toString();
                        }
                        

                        Which is faster as using query.value("name").toString()

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        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