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. read a value from QSql database
Forum Updated to NodeBB v4.3 + New Features

read a value from QSql database

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 426 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #1

    I just created a database table, it looks like this

    QSqlQuery query("CREATE TABLE tools (ind INTEGER, nom TEXT, longueur REAL, diametre REAL, dureeVieMinutes INTEGER, dureeUsageMinutes INTEGER)");
    
    

    i wrote one methods for insert a tool and one edit a property from existing tool

    i want to implement a method to read single property but i have troubles to do it

    qreal ToolDBManager::getToolLength(QString _toolName){
        qreal length = -999; // return this if we cant read the length
    
        QSqlQuery query;
        query.prepare("SELECT longueur FROM tools WHERE nom=:toolName");
        query.bindValue(":toolName",_toolName);
    
        bool toRealOk = false; 
    
        if(query.exec()){
    
           length = query.record().value("longueur").toReal(&toRealOk); 
           qDebug()<< "LENGTH IS : " << length;
    
        }
        else
        {
             qDebug() << "getToolLength error:  "
                      << query.lastError();
        }
      
    }
    

    i alwase get 0.., witch is not the correct value set in the db
    is there something not correct in this line

    length = query.record().value("longueur").toReal(&toRealOk); 
    

    can someone help please ?
    thx

    JonBJ 1 Reply Last reply
    0
    • ODБOïO ODБOï

      I just created a database table, it looks like this

      QSqlQuery query("CREATE TABLE tools (ind INTEGER, nom TEXT, longueur REAL, diametre REAL, dureeVieMinutes INTEGER, dureeUsageMinutes INTEGER)");
      
      

      i wrote one methods for insert a tool and one edit a property from existing tool

      i want to implement a method to read single property but i have troubles to do it

      qreal ToolDBManager::getToolLength(QString _toolName){
          qreal length = -999; // return this if we cant read the length
      
          QSqlQuery query;
          query.prepare("SELECT longueur FROM tools WHERE nom=:toolName");
          query.bindValue(":toolName",_toolName);
      
          bool toRealOk = false; 
      
          if(query.exec()){
      
             length = query.record().value("longueur").toReal(&toRealOk); 
             qDebug()<< "LENGTH IS : " << length;
      
          }
          else
          {
               qDebug() << "getToolLength error:  "
                        << query.lastError();
          }
        
      }
      

      i alwase get 0.., witch is not the correct value set in the db
      is there something not correct in this line

      length = query.record().value("longueur").toReal(&toRealOk); 
      

      can someone help please ?
      thx

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

      @LeLev
      Let's start with:

      • You're supposed to check if you got a row back. Just because the exec() succeeds does not mean it found a row. In which case, I suspect your query.record() would still be the "empty" record for the table.

      • You're supposed to check what is in toRealOk after .toReal(&toRealOk), to tell you whether the conversion succeeded. I don't see you do that.

      • To debug, start by just printing out what you get back from query.record().value("longueur"), without trying to toReal() it.

      These apart, my guess is:
      https://doc.qt.io/qt-5/qvariant.html#toReal

      Returns the variant as a qreal if the variant has userType() QMetaType::Double, QMetaType::Float, QMetaType::Bool, QMetaType::QByteArray, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.0.

      At some level, the database table's REAL perhaps does not correspond to/has not been converted one of these Qt types (print out the QVariant type?). You have to look up how that bit works, database types != code types.

      ODБOïO 1 Reply Last reply
      6
      • JonBJ JonB

        @LeLev
        Let's start with:

        • You're supposed to check if you got a row back. Just because the exec() succeeds does not mean it found a row. In which case, I suspect your query.record() would still be the "empty" record for the table.

        • You're supposed to check what is in toRealOk after .toReal(&toRealOk), to tell you whether the conversion succeeded. I don't see you do that.

        • To debug, start by just printing out what you get back from query.record().value("longueur"), without trying to toReal() it.

        These apart, my guess is:
        https://doc.qt.io/qt-5/qvariant.html#toReal

        Returns the variant as a qreal if the variant has userType() QMetaType::Double, QMetaType::Float, QMetaType::Bool, QMetaType::QByteArray, QMetaType::Int, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.0.

        At some level, the database table's REAL perhaps does not correspond to/has not been converted one of these Qt types (print out the QVariant type?). You have to look up how that bit works, database types != code types.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        hi @JonB thank you very much

           if(query.exec()){
                qDebug()<< "RECORD VALUE IS : " <<  query.record().value("longueur");
        

        this will output

        RECORD VALUE IS :  QVariant(double, 0)
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          It might not be placed on a valid row yet. (as @JonB also talk about)
          The normal way as docs shows is

             QSqlQuery query("SELECT country FROM artist");
              while (query.next()) {
                  QString country = query.value(0).toString();
                  doSomething(country);
              }
          

          where you call query.next() to place it on row.

          ODБOïO 1 Reply Last reply
          6
          • mrjjM mrjj

            Hi
            It might not be placed on a valid row yet. (as @JonB also talk about)
            The normal way as docs shows is

               QSqlQuery query("SELECT country FROM artist");
                while (query.next()) {
                    QString country = query.value(0).toString();
                    doSomething(country);
                }
            

            where you call query.next() to place it on row.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #5

            @mrjj said in read a value from QSql database:

            next()

            yes exactly, thank you 2

            qreal ToolDBManager::getToolLength(QString _toolName){
                qreal length = -999;
            
                QSqlQuery query;
                query.prepare("SELECT longueur FROM tools WHERE nom=:toolName");
                query.bindValue(":toolName",_toolName);
            
                bool toRealOk = false;
            
                if(query.exec()){
            
                    while (query.next()) {
            
                        length = query.record().value("longueur").toReal(&toRealOk);
            
                        if(!toRealOk) return -999;
                        
                    }
                }
                else
                {
                    qDebug() << "getToolLength error:  "
                             << query.lastError();
                }
                return length;
            }
            

            thank you very much @JonB @mrjj

            1 Reply Last reply
            3

            • Login

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