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. Count QSqlquery results!!
Qt 6.11 is out! See what's new in the release blog

Count QSqlquery results!!

Scheduled Pinned Locked Moved General and Desktop
20 Posts 10 Posters 42.3k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #4

    bq. Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.

    Not every database reports back the number of rows in a result set. If you want to be database independent you should not rely on this value.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      absfrm
      wrote on last edited by
      #5

      why my size always return -1???
      this is my code :
      @
      if (mydb.open())
      {
      QSqlQuery query("SELECT * FROM content WHERE Title like '"+keyword+"'");
      int titleNo = query.record().indexOf("Title");
      int textNo = query.record().indexOf("Text");
      int authorNo = query.record().indexOf("Author");
      while (query.next()) {
      if(query.size()>0){
      title = query.value(titleNo).toString();
      text = query.value(textNo).toString();
      author = query.value(authorNo).toString();
      test = "<h1>" + title + "</h1><br><p>" + text + "</p><br>" + author;}
      }
      else {
      (do somthing);
      }
      mydb.close();

      }
      @

      If You Want You Can!

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hostel
        wrote on last edited by
        #6

        About size = -1 see a Volker's post. Maybe your database not support this. When you are using a:
        @
        while( query.next() )
        {
        // code
        }
        @
        Then you have in your query records, so you have not to check the size.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          absfrm
          wrote on last edited by
          #7

          I'm using mysqli . support it?
          if no , how can i get the size or number of row in results?

          i used
          @

          query.numRowsAffected()

          @

          too
          but its not worked!

          If You Want You Can!

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hostel
            wrote on last edited by
            #8

            You can check if driver support feature by "hasFeature method":http://developer.qt.nokia.com/doc/qt-4.8/qsqldriver.html#hasFeature.

            If you need a result count after while loop then you can do this:
            @
            int recCount = 0;
            while( query.next() )
            {
            recCount++;
            // code
            }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #9

              If all you need is to distinguish the cases where you have 0, 1 or >1 results (that's what I gather from your opening post), then you don't need to count the results at all. Counting by iterating through the result set is potentially very expensive, so I would avoid it.

              Just perform your query, and try to get the first result. If you don't have any results, you know at this point. If there is a result, cache the values in an object, and try to get the next result. If that works, you have >1 result, if not, you have only 1. No counting needed at all.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                absfrm
                wrote on last edited by
                #10

                hi
                thanks Hostel.i tried it.

                so thanks Andre. Excellent answer.

                ;) be happy

                If You Want You Can!

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tucnak
                  wrote on last edited by
                  #11

                  You should write something:
                  @
                  I will read documentation.
                  I will read documentation.
                  I will read documentation.
                  I will read documentation.
                  I will read documentation.
                  I will read documentation.
                  I will read documentation.
                  I will read documentation.
                  @

                  P.S. Use <code>int numRowsAffected()</code>

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #12

                    [quote author="abbas farahmand" date="1326023459"]I'm using mysqli . support it?
                    if no , how can i get the size or number of row in results?

                    i used
                    @

                    query.numRowsAffected()

                    @

                    too
                    but its not worked![/quote]

                    [quote author="tucnak" date="1326032692"]You should write something:
                    @
                    I will read documentation.
                    I will read documentation.
                    I will read documentation.
                    I will read documentation.
                    I will read documentation.
                    I will read documentation.
                    I will read documentation.
                    I will read documentation.
                    @

                    P.S. Use <code>int numRowsAffected()</code>[/quote]

                    Conclusion:
                    [quote]
                    I will read previous posts before replying.
                    ...
                    [/quote]

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on last edited by
                      #13

                      [quote author="tucnak" date="1326032692"]You should write something:
                      @
                      I will read documentation.
                      I will read documentation.
                      I will read documentation.
                      I will read documentation.
                      I will read documentation.
                      I will read documentation.
                      I will read documentation.
                      I will read documentation.
                      @

                      P.S. Use <code>int numRowsAffected()</code>[/quote]

                      Let me now think about your proposal and read the numRowsAffected documentation:
                      [quote]
                      Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for SELECT statements, the value is undefined; use size() instead.
                      [/quote]

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #14

                        Yeah, there's still a little difference between reading documentation and understanding documentation...

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          absfrm
                          wrote on last edited by
                          #15

                          hi again
                          Andre answer me excellent ,that's OK.
                          why did you reply again?

                          be happy.
                          thanks all.

                          If You Want You Can!

                          1 Reply Last reply
                          0
                          • Z Offline
                            Z Offline
                            Zarkon
                            wrote on last edited by
                            #16

                            Thanks for this post. I was just wrestling with this problem.

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #17

                              For anyone with the same problem, I used to get the rows length with:

                              @query->record().count()@

                              I hope may help someone.

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #18

                                For anyone with the same problem, I used to get the rows length with:

                                @query->record().count()@

                                I hope may help someone.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  Hi and welcome to devnet,

                                  Row length and row count are not the same thing. The row length is the number of field in the row, while the row count is the number of row returned by your query.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #20

                                    Hi and welcome to devnet,

                                    Row length and row count are not the same thing. The row length is the number of field in the row, while the row count is the number of row returned by your query.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0

                                    • Login

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