QSqlTableModel::rowCount && Windows problem
-
wrote on 3 Sept 2010, 18:46 last edited by
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?
-
wrote on 3 Sept 2010, 18:53 last edited by
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?
-
wrote on 4 Sept 2010, 16:50 last edited by
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();
}@ -
wrote on 5 Sept 2010, 12:45 last edited by
[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.
-
wrote on 6 Sept 2010, 06:38 last edited by
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.
-
wrote on 6 Sept 2010, 07:59 last edited by
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.
1/6