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. Counting records in a QSqLite db
Forum Updated to NodeBB v4.3 + New Features

Counting records in a QSqLite db

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 5.1k Views 4 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.
  • G Offline
    G Offline
    gabor53
    wrote on 9 Aug 2017, 03:00 last edited by
    #1

    Hi,
    I have a QSqLite db and I need to find out the number of records in one of the tables. I currently have the following code, which says I have only one record (the correct number would be 7):

      while (query_fix.next () == true) {
        recCount++;
        query_fix.next ();
      }
      qDebug() << "Record count (fixdb): " << recCount;
    

    What did I miss?
    Thank you for your help.

    Y V 2 Replies Last reply 9 Aug 2017, 03:38
    0
    • G gabor53
      9 Aug 2017, 03:00

      Hi,
      I have a QSqLite db and I need to find out the number of records in one of the tables. I currently have the following code, which says I have only one record (the correct number would be 7):

        while (query_fix.next () == true) {
          recCount++;
          query_fix.next ();
        }
        qDebug() << "Record count (fixdb): " << recCount;
      

      What did I miss?
      Thank you for your help.

      Y Offline
      Y Offline
      Yashpal
      wrote on 9 Aug 2017, 03:38 last edited by
      #2

      @gabor53 said in Counting records in a QSqLite db:

      query_fix.next ();

      Why are you having query_fix.next (); inside while loop? Remove them, post that you should record count correctly.

      1 Reply Last reply
      2
      • G gabor53
        9 Aug 2017, 03:00

        Hi,
        I have a QSqLite db and I need to find out the number of records in one of the tables. I currently have the following code, which says I have only one record (the correct number would be 7):

          while (query_fix.next () == true) {
            recCount++;
            query_fix.next ();
          }
          qDebug() << "Record count (fixdb): " << recCount;
        

        What did I miss?
        Thank you for your help.

        V Offline
        V Offline
        Venkatesh V
        wrote on 9 Aug 2017, 04:02 last edited by
        #3

        @gabor53
        Hi,
        try this query,
        SELECT Count(*) FROM TableName;

        G 1 Reply Last reply 9 Aug 2017, 19:39
        2
        • V Venkatesh V
          9 Aug 2017, 04:02

          @gabor53
          Hi,
          try this query,
          SELECT Count(*) FROM TableName;

          G Offline
          G Offline
          gabor53
          wrote on 9 Aug 2017, 19:39 last edited by
          #4

          @Venkatesh-V
          I tried like this:

          recCount =  query_fix("SELECT COUNT(*) FROM Items");
          

          and I got the following error message:
          C:\Programming\Projects\Folkfriends_bzr\checkout\fixdb.cpp:121: error: no match for call to '(QSqlQuery) (const char [27])'
          recCount = query_fix("SELECT COUNT(*) FROM Items");
          ^
          Any idea why?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 9 Aug 2017, 22:01 last edited by
            #5

            Hi,

            Because that's not how you use a QSqlQuery object.

            A select count should be handled the same as classic select call. You'll just have one result which will contain the number of rows.

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

            G 1 Reply Last reply 10 Aug 2017, 00:18
            1
            • S SGaist
              9 Aug 2017, 22:01

              Hi,

              Because that's not how you use a QSqlQuery object.

              A select count should be handled the same as classic select call. You'll just have one result which will contain the number of rows.

              G Offline
              G Offline
              gabor53
              wrote on 10 Aug 2017, 00:18 last edited by
              #6

              @SGaist
              Hi,
              Is this supposed to work on QSqLite?

              V 1 Reply Last reply 10 Aug 2017, 04:11
              0
              • G gabor53
                10 Aug 2017, 00:18

                @SGaist
                Hi,
                Is this supposed to work on QSqLite?

                V Offline
                V Offline
                Venkatesh V
                wrote on 10 Aug 2017, 04:11 last edited by
                #7

                @gabor53
                Hi,

                int recCount = query_fix.prepare("SELECT COUNT(*) FROM Items");
                query_fix.exec();

                G 1 Reply Last reply 10 Aug 2017, 13:13
                1
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 10 Aug 2017, 07:14 last edited by
                  #8

                  @gabor53 What is supposed to work ?

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

                  G 1 Reply Last reply 10 Aug 2017, 13:07
                  0
                  • S SGaist
                    10 Aug 2017, 07:14

                    @gabor53 What is supposed to work ?

                    G Offline
                    G Offline
                    gabor53
                    wrote on 10 Aug 2017, 13:07 last edited by
                    #9

                    @SGaist
                    select count with QSqLite.

                    1 Reply Last reply
                    0
                    • V Venkatesh V
                      10 Aug 2017, 04:11

                      @gabor53
                      Hi,

                      int recCount = query_fix.prepare("SELECT COUNT(*) FROM Items");
                      query_fix.exec();

                      G Offline
                      G Offline
                      gabor53
                      wrote on 10 Aug 2017, 13:13 last edited by gabor53 8 Oct 2017, 13:17
                      #10

                      @Venkatesh-V
                      This returns 1, but there are 7 records in the db. Any idea why?

                       int recNum;
                        recNum = query_fix.prepare ("SELECT COUNT(*) FROM Items");
                        query_fix.exec ();
                      
                        qDebug() << "Record count in fixdb: " << recNum;
                      
                      mrjjM 1 Reply Last reply 10 Aug 2017, 13:26
                      0
                      • G gabor53
                        10 Aug 2017, 13:13

                        @Venkatesh-V
                        This returns 1, but there are 7 records in the db. Any idea why?

                         int recNum;
                          recNum = query_fix.prepare ("SELECT COUNT(*) FROM Items");
                          query_fix.exec ();
                        
                          qDebug() << "Record count in fixdb: " << recNum;
                        
                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 10 Aug 2017, 13:26 last edited by mrjj 8 Oct 2017, 13:28
                        #11

                        hi
                        If i show you how the function is defined, can you then guess why its 1 ?
                        bool QSqlQuery::prepare(const QString & query)

                        I will give you a hint and say that
                        query_fix.exec ();
                        must be used.
                        Also the value of true is often 1.

                        G 1 Reply Last reply 10 Aug 2017, 13:39
                        1
                        • mrjjM mrjj
                          10 Aug 2017, 13:26

                          hi
                          If i show you how the function is defined, can you then guess why its 1 ?
                          bool QSqlQuery::prepare(const QString & query)

                          I will give you a hint and say that
                          query_fix.exec ();
                          must be used.
                          Also the value of true is often 1.

                          G Offline
                          G Offline
                          gabor53
                          wrote on 10 Aug 2017, 13:39 last edited by
                          #12

                          @mrjj
                          Thank you.Now I understand why it is 1. How can I get around it?

                          V 1 Reply Last reply 10 Aug 2017, 14:39
                          0
                          • G gabor53
                            10 Aug 2017, 13:39

                            @mrjj
                            Thank you.Now I understand why it is 1. How can I get around it?

                            V Offline
                            V Offline
                            Venkatesh V
                            wrote on 10 Aug 2017, 14:39 last edited by
                            #13

                            @gabor53

                            Hi,
                            Try this,

                            int recNum =0;
                            query_fix.prepare ("SELECT * FROM Items");
                            if( query_fix.exec ()){
                            while( query.next() )
                            {
                            recNum ++;
                            }
                            }
                            qDebug() << "Record count in fixdb: " << recNum;

                            1 Reply Last reply
                            3
                            • V Offline
                              V Offline
                              Venkatesh V
                              wrote on 10 Aug 2017, 14:44 last edited by
                              #14

                              Or you can also use this,

                              QSqlQuery q;
                              q.prepare("SELECT COUNT (*) FROM TableName");
                              q.exec();
                              int rows= 0;
                              if (q.next()) {
                              rows= q.value(0).toInt();
                              }

                              G 1 Reply Last reply 11 Aug 2017, 02:29
                              5
                              • V Venkatesh V
                                10 Aug 2017, 14:44

                                Or you can also use this,

                                QSqlQuery q;
                                q.prepare("SELECT COUNT (*) FROM TableName");
                                q.exec();
                                int rows= 0;
                                if (q.next()) {
                                rows= q.value(0).toInt();
                                }

                                G Offline
                                G Offline
                                gabor53
                                wrote on 11 Aug 2017, 02:29 last edited by
                                #15

                                @Venkatesh-V
                                This worked.
                                Thank you.

                                1 Reply Last reply
                                1

                                1/15

                                9 Aug 2017, 03:00

                                • Login

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