Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlTableModel::rowCount && Windows problem
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel::rowCount && Windows problem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 7.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luca
    wrote on 3 Sept 2010, 18:46 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luca
      wrote on 3 Sept 2010, 18:53 last edited by
      #2

      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?

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xeonn
        wrote on 4 Sept 2010, 16:50 last edited by
        #3

        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();
        }@

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on 5 Sept 2010, 12:45 last edited by
          #4

          [quote author="Onn Khairuddin Ismail" date="1283619053"]I think, faster way is to do

          @int rowCount()
          {
          QSqlQuery qry;
          if(qry.exec(select count(*&#41; 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.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Franzk
            wrote on 6 Sept 2010, 06:38 last edited by
            #5

            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.

            "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • X Offline
              X Offline
              xeonn
              wrote on 6 Sept 2010, 07:59 last edited by
              #6

              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 Reply Last reply
              0

              1/6

              3 Sept 2010, 18:46

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved