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!!
QtWS25 Last Chance

Count QSqlquery results!!

Scheduled Pinned Locked Moved General and Desktop
20 Posts 10 Posters 38.3k Views
  • 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.
  • A Offline
    A Offline
    absfrm
    wrote on 7 Jan 2012, 20:18 last edited by
    #1

    hi to all
    I want to count my results of query

    for Example :
    i have 3 same record in my SQL database in title column
    and i want to search to it. if my titles searched by keyword has one result (do something). else .... (do something)
    @
    QSqlQuery query("SELECT * FROM ... WHERE ... ...");
    query.exec;
    //
    if query has more than one result , (do something)

    // and result was unique
    else {

    }
    @

    thanks.

    If You Want You Can!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 7 Jan 2012, 20:22 last edited by
      #2

      Is the "size":http://developer.qt.nokia.com/doc/qt-4.8/qsqlquery.html#size method not sufficient?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • A Offline
        A Offline
        absfrm
        wrote on 7 Jan 2012, 20:27 last edited by
        #3

        oh i forgot it
        so thanks ...

        If You Want You Can!

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on 7 Jan 2012, 23:12 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.

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            absfrm
            wrote on 8 Jan 2012, 06:31 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 8 Jan 2012, 11:01 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 8 Jan 2012, 11:50 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 8 Jan 2012, 13:38 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 8 Jan 2012, 14:06 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 8 Jan 2012, 14:14 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 8 Jan 2012, 14:24 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 8 Jan 2012, 14:40 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 8 Jan 2012, 14:50 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
                            • G Offline
                              G Offline
                              goetz
                              wrote on 8 Jan 2012, 14:56 last edited by
                              #14

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

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

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                absfrm
                                wrote on 9 Jan 2012, 05:47 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 17 Jul 2014, 10:43 last edited by
                                  #16

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

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    Atron
                                    wrote on 4 Dec 2014, 09:32 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
                                    • A Offline
                                      A Offline
                                      Atron
                                      wrote on 4 Dec 2014, 09:32 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 5 Dec 2014, 00:24 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 5 Dec 2014, 00:24 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