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.
  • S SPlatten
    20 Oct 2021, 13:00

    @jsulm , I've modified the query function:

    bool Trainer::queryDB(QSqlQuery& rQuery, bool blnIgnoreError)
    {
        bool blnSuccess = rQuery.exec();
    qDebug() << rQuery.executedQuery().toLatin1().data();
        if ( blnSuccess != true && blnIgnoreError == false )
        {       
        //Query wasn't successful, get last error
            QSqlError err = rQuery.lastError();
            if ( err.type() != QSqlError::NoError )
            {
                ImportRDF* pImportDialog(ImportRDF::pInstance());
                logError(err);
                if ( pImportDialog != nullptr )
                {
                    pImportDialog->closeDialog();
                }
            }
        }
        return blnSuccess;
    }
    

    For the query I'm having a problem with the output is:

    SELECT  `binChunk` FROM `rdf` WHERE `biDataset`=? AND `intBlockNo`=?
    

    Obviously it isn't including the parameters I've supplied.

    [Edit] Modified again and added:

    bool Trainer::queryDB(QSqlQuery& rQuery, bool blnIgnoreError)
    {
        bool blnSuccess = rQuery.exec();
    qDebug() << rQuery.executedQuery().toLatin1().data();
    qDebug() << rQuery.lastError().number();
            if ( blnSuccess != true && blnIgnoreError == false )
        {       
        //Query wasn't successful, get last error
            QSqlError err = rQuery.lastError();
            if ( err.type() != QSqlError::NoError )
            {
                ImportRDF* pImportDialog(ImportRDF::pInstance());
                logError(err);
                if ( pImportDialog != nullptr )
                {
                    pImportDialog->closeDialog();
                }
            }
        }
        return blnSuccess;
    }
    

    Error number is always -1, what does that mean?

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 20 Oct 2021, 13:06 last edited by
    #4

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

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

    S 3 Replies Last reply 20 Oct 2021, 13:08
    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:08 last edited by
      #5

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

      Kind Regards,
      Sy

      J 1 Reply Last reply 20 Oct 2021, 13:12
      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: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 Offline
          J Offline
          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 Offline
                J Offline
                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 Offline
                    J Offline
                    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 Offline
                        J Offline
                        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

                            13/16

                            20 Oct 2021, 13:22

                            • Login

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