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.9k 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.
  • J jsulm
    20 Oct 2021, 13:06

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

    S Offline
    S Offline
    SPlatten
    wrote on 20 Oct 2021, 13:09 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
    • S SPlatten
      20 Oct 2021, 13:08

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

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 20 Oct 2021, 13:12 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

      S 1 Reply Last reply 20 Oct 2021, 13:14
      0
      • J jsulm
        20 Oct 2021, 13:06

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

        S Offline
        S Offline
        SPlatten
        wrote on 20 Oct 2021, 13:12 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

        J 1 Reply Last reply 20 Oct 2021, 13:18
        0
        • J jsulm
          20 Oct 2021, 13:12

          @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?

          S Offline
          S Offline
          SPlatten
          wrote on 20 Oct 2021, 13:14 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

          J 1 Reply Last reply 20 Oct 2021, 13:20
          0
          • S SPlatten
            20 Oct 2021, 13:12

            @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());
            
            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 20 Oct 2021, 13:18 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

            S 1 Reply Last reply 20 Oct 2021, 13:19
            0
            • J jsulm
              20 Oct 2021, 13:18

              @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.

              S Offline
              S Offline
              SPlatten
              wrote on 20 Oct 2021, 13:19 last edited by
              #11

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

              Kind Regards,
              Sy

              J 1 Reply Last reply 20 Oct 2021, 13:23
              0
              • S SPlatten
                20 Oct 2021, 13:14

                @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.

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 20 Oct 2021, 13:20 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

                S 1 Reply Last reply 20 Oct 2021, 13:22
                0
                • J jsulm
                  20 Oct 2021, 13:20

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

                  S Offline
                  S Offline
                  SPlatten
                  wrote on 20 Oct 2021, 13:22 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
                  • S SPlatten
                    20 Oct 2021, 13:19

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

                    J Online
                    J Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 20 Oct 2021, 13:23 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

                    S 1 Reply Last reply 20 Oct 2021, 13:28
                    2
                    • J jsulm
                      20 Oct 2021, 13:23

                      @SPlatten Do you get valid data with this:

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

                      ?

                      S Offline
                      S Offline
                      SPlatten
                      wrote on 20 Oct 2021, 13:28 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

                      K 1 Reply Last reply 20 Oct 2021, 13:43
                      0
                      • S SPlatten
                        20 Oct 2021, 13:28

                        @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.

                        K Offline
                        K Offline
                        KroMignon
                        wrote on 20 Oct 2021, 13:43 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

                        15/16

                        20 Oct 2021, 13:28

                        • Login

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