Solved 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 linelength = 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 yourquery.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 totoReal()
it.
These apart, my guess is:
https://doc.qt.io/qt-5/qvariant.html#toRealReturns 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 theQVariant
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)
-
Hi
It might not be placed on a valid row yet. (as @JonB also talk about)
The normal way as docs shows isQSqlQuery 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; }