QSqlTableModel::rowCount && Windows problem
-
Hi all,
I've got a problem with QSqlTableModel::rowCount() in a windows application.
This is the code:
@
{
QSqlQuery query;
query.exec("some query...");
QSqlTableModel table_model;
table_model.setQuery(query);
qDebug() << table_model.rowCount();
}
@the query result has 500 rows.
Under Linux "table_model.rowCount()" give me the number "500" but under windows it give me the number "256".
To get the real rows number under windows I must count it from the executed query:
@
int count=0;
while(query.next())
count++;
@In this way I get the real number.
Do you know a way to avoid this problem under windows?
-
I found another solution In this post:
"http://www.qtforum.org/article/22611/qsqltablemodel-rowcount.html":http://www.qtforum.org/article/22611/qsqltablemodel-rowcount.html
@
while(model->canFetchMore())
model->fetchMore();
model->rowCount();
@What solution do you think is less expensive?
-
[quote author="Onn Khairuddin Ismail" date="1283619053"]I think, faster way is to do
@int rowCount()
{
QSqlQuery qry;
if(qry.exec(select count(*) from [tablename] where .. your original filter .. ) && qry.next())
return qry.record().value(0).toInt();
}@[/quote]Thanks but I'd like to avoid the double execution of the query because it is a complex query (with some join) so it require some time to be executed from the db server.
-
How about a bit of caching?
@int rowCount()
{
if (m_rowCount >= 0)
return m_rowCount;
QSqlQuery qry;
if(qry.exec(select count(*) from [tablename] where .. your original filter .. ) && qry.next())
return m_rowCount = qry.record().value(0).toInt();
return 0;
}@In the end you probably have to go for the canFetchMore() solution, because rowCount() is called VERY often.
-
canFetchMore() will make a round trip to server as well. And it might get uglier than "select count(*)" since the hint indicate you should fetch more records to get accurate record count. More round trip for huge database of 100,000,000 records.
What database are you using? Mysql ISAAM will provide instantaneous response on "select count(*)" as it is cached. Mysql InnoDB and postgresql count the record everytime, thus "select count" is a little slow (Not really that noticeable).
Bottom line is, round trip to server is unavoidable. Franzk's addition by caching, looks marvelous. I will go with that.