Unsolved QSqlQuery numRowsAffected() vs. size()
-
Dear All,
I have been playing with SQL capabilities of Qt. According to the documentation (https://doc.qt.io/qt-6/qsqlquery.html), for select statements, size() returns the number of rows returned, and numRowsAffected() is not defined. But this seems incorrect from my experiments on a simple select statement (using QODBC connecting to MS-SQL 2022), the numRowsAffected() returns the number of rows returned from the select statement, and size() returns -1. Is it possible there is a bug in Qt or in the documentation?Sincerely,.
Yo-Ming Hsieh -
@ymhsieh said in QSqlQuery numRowsAffected() vs. size():
nd size() returns -1. Is it possible there is a bug in Qt or in the documentation?
No, but you did not read the doc completely: "Note that for SELECT statements, the value is undefined; use size() instead. If the query is not active, -1 is returned."
-
@Christian-Ehrlicher
"No, but you did not read the doc completely: "Note that for SELECT statements, the value is undefined; use size() instead. If the query is not active, -1 is returned."
This is exactly the statement that confuses me. I am pretty certain that I have an active select statement (because I can get my rows), so size() should give me the number of rows, but it returns -1. While numRowsAffected() should be undefined, but it gives me exactly the number of rows that I can retrieve... :-( -
Well, in C++ there is no such value as
undefined
. "Undefined" just means that what value should be returned is not defined in design.
So it may return the value you want, or it may return invalid value, or any value, usually we should not depend on that.As for the
size()
function, the doc also write that it returns-1 if the size cannot be determined or if the database does not support reporting information about query sizes
In your case, ODBC databases do not support reporting the size.
You can confirm that by callingdriver()->hasFeature(QSqlDriver::QuerySize)
. -
@Bonnie Great, thank you for your explanation, although I still find it weird that numRowsAffected() gives me the number of rows ...XD