Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

read a value from QSql database



  • 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



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



  • 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)
    

  • Lifetime Qt Champion

    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.



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


Log in to reply