PDA

View Full Version : Question about method named rowCount() of QSqlTableModel



silentyears
21st January 2014, 10:17
Hi all,
After i use QSqlTableModel to get one table in database, i want to handle every row in table ,so i code as follows:


QSqlTableModel tableModel;
tableModel->setTable(tableA);
tableModel->select();
int nCount = tableModel->rowCount();
for(int i=0, i<nCount, i++)
{
// handle every row
// ....
}

here is my question, why the value of nCount is less than the real rows' count(Observed from database table view)? And the value is 256, could i get whole rows when the count of rows are so many like tens of thousands? Or is there any smarter idea to do the same work?
Thank you!

StrikeByte
21st January 2014, 11:55
it is possible that nCount is different than the real rows in the database (this behaviour is described in the manual)
from the manual:
If the database supports returning the size of a query (see QSqlDriver::hasFeature()), the number of rows of the current query is returned. Otherwise, returns the number of rows currently cached on the client.

if there is a smarter way, depends on what you try to accomplish

silentyears
21st January 2014, 15:59
it is possible that nCount is different than the real rows in the database (this behaviour is described in the manual)
from the manual:
If the database supports returning the size of a query (see QSqlDriver::hasFeature()), the number of rows of the current query is returned. Otherwise, returns the number of rows currently cached on the client.

if there is a smarter way, depends on what you try to accomplish

Thank you so much!
could you tell me where the behaviour is described ? i can't find in the help doc, maybe i'm not careful enough.
I use Oracle database, if it supports returning the size of a query and how?
I also think if i can change the cached to get more number of rows, but it doesn't seem a good way...

Thx again!

StrikeByte
22nd January 2014, 10:04
The behaviour is described in http://qt-project.org/doc/qt-4.8/qsqlquerymodel.html#rowCount
To see if it supports returning the size you can use the hasFeature() function (there is a link to the function in the rowcount documentation)
To get all rows you can do a sql select that returns all rows (SELECT * FROM tablename) you can replace * with a column name u need

If you don't mind me asking why do you want to get a count of all rows?

silentyears
14th February 2014, 06:53
The behaviour is described in http://qt-project.org/doc/qt-4.8/qsqlquerymodel.html#rowCount
To see if it supports returning the size you can use the hasFeature() function (there is a link to the function in the rowcount documentation)
To get all rows you can do a sql select that returns all rows (SELECT * FROM tablename) you can replace * with a column name u need

If you don't mind me asking why do you want to get a count of all rows?
Thank you first! sorry for reply late,I just see it。
To get all rows,I can handle every row though a loop(eg· for(int i=0;i<rowcount;i++))
Is there any better idea to traverse a table without rowcount of qsqltablemodel?thanks!

anda_skoa
14th February 2014, 09:01
You can use QSqlQuery directly.

Cheers,
_