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.2k 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 gabor53

    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.

    YashpalY Offline
    YashpalY Offline
    Yashpal
    wrote on 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

      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.

      Venkatesh VV Offline
      Venkatesh VV Offline
      Venkatesh V
      wrote on last edited by
      #3

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

      G 1 Reply Last reply
      2
      • Venkatesh VV Venkatesh V

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

        G Offline
        G Offline
        gabor53
        wrote on 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          1
          • SGaistS SGaist

            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 last edited by
            #6

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

            Venkatesh VV 1 Reply Last reply
            0
            • G gabor53

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

              Venkatesh VV Offline
              Venkatesh VV Offline
              Venkatesh V
              wrote on last edited by
              #7

              @gabor53
              Hi,

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

              G 1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                0
                • SGaistS SGaist

                  @gabor53 What is supposed to work ?

                  G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #9

                  @SGaist
                  select count with QSqLite.

                  1 Reply Last reply
                  0
                  • Venkatesh VV Venkatesh V

                    @gabor53
                    Hi,

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

                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by gabor53
                    #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
                    0
                    • G gabor53

                      @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 last edited by mrjj
                      #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
                      1
                      • mrjjM mrjj

                        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 last edited by
                        #12

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

                        Venkatesh VV 1 Reply Last reply
                        0
                        • G gabor53

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

                          Venkatesh VV Offline
                          Venkatesh VV Offline
                          Venkatesh V
                          wrote on 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
                          • Venkatesh VV Offline
                            Venkatesh VV Offline
                            Venkatesh V
                            wrote on 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
                            5
                            • Venkatesh VV Venkatesh V

                              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 last edited by
                              #15

                              @Venkatesh-V
                              This worked.
                              Thank you.

                              1 Reply Last reply
                              1

                              • Login

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