Solved QSqlQuery error handling
-
@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?
-
@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());
-
@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.
-
@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.
-
@jsulm , ok, I can remove that, but that doesn't explain the problem.
-
@SPlatten Also please read what https://doc.qt.io/qt-5.15/qsqlrecord.html#count actually returns
-
@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.
-
@SPlatten Do you get valid data with this:
while (query.next()) { QByteArray data = query.value(0).toByteArray(); ... }
?
-
@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.
-
@SPlatten AFAIK,
QSqlQuery::record()
is to be used afterQSqlQuery::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()