Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. QT SQL query loop?
Forum Update on Monday, May 27th 2025

QT SQL query loop?

Scheduled Pinned Locked Moved Unsolved Brainstorm
8 Posts 3 Posters 2.6k 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.
  • D Offline
    D Offline
    davethedave
    wrote on 11 Aug 2018, 17:29 last edited by davethedave 8 Nov 2018, 17:29
    #1

    Hey, currently this loop only gets the first username in the table, how can i properly do this so it checks all rows for the username
    while (query.next() && query.value("Username").toString().toUtf8().constData() && query.value("Password").toString().toUtf8().constData())
    {
    printf("Usernames: %s", query.value("Username").toString().toUtf8().constData());

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Aug 2018, 18:50 last edited by
      #2

      Hi,

      How many entries do you have in that table ?
      What query did you use ?
      Why are you doing this complex check in the while condition ?

      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
      • D Offline
        D Offline
        davethedave
        wrote on 12 Aug 2018, 10:32 last edited by
        #3

        Hello, I have 5 entries
        query.exec("SELECT * FROM users");
        if (query.size() > 0)

        and its the only check i could think of

        J 1 Reply Last reply 12 Aug 2018, 10:35
        0
        • D davethedave
          12 Aug 2018, 10:32

          Hello, I have 5 entries
          query.exec("SELECT * FROM users");
          if (query.size() > 0)

          and its the only check i could think of

          J Offline
          J Offline
          JonB
          wrote on 12 Aug 2018, 10:35 last edited by
          #4

          @davethedave

          query.exec("SELECT * FROM users");
          while (query.next())
          {
              printf("Usernames: %s", query.value("Username").toString());
          }
          
          D 1 Reply Last reply 12 Aug 2018, 11:56
          0
          • J JonB
            12 Aug 2018, 10:35

            @davethedave

            query.exec("SELECT * FROM users");
            while (query.next())
            {
                printf("Usernames: %s", query.value("Username").toString());
            }
            
            D Offline
            D Offline
            davethedave
            wrote on 12 Aug 2018, 11:56 last edited by
            #5

            @JonB
            Prints first entry in the table still.

            J 1 Reply Last reply 12 Aug 2018, 15:13
            0
            • D davethedave
              12 Aug 2018, 11:56

              @JonB
              Prints first entry in the table still.

              J Offline
              J Offline
              JonB
              wrote on 12 Aug 2018, 15:13 last edited by
              #6

              @davethedave
              Please show output of each of the 2 following:

              query.exec("SELECT COUNT(*) FROM users");
              while (query.next())
              {
                  printf("Number: %s\n", query.value(0).toString());
              }
              
              query.exec("SELECT * FROM users");
              while (query.next())
              {
                  printf("Username: %s\n", query.value(0).toString());
              }
              
              1 Reply Last reply
              0
              • D Offline
                D Offline
                davethedave
                wrote on 12 Aug 2018, 15:24 last edited by davethedave 8 Dec 2018, 15:25
                #7

                @JonB said in QT SQL query loop?:

                query.exec("SELECT * FROM users");

                from the first:
                http://prntscr.com/khp72u

                if i append
                .toUtf8().constData() then the below
                http://prntscr.com/khp79y

                from the second:
                http://prntscr.com/khp72u

                if i append
                .toUtf8().constData() then the below
                http://prntscr.com/khp7tw

                Also there is only 3 rows now

                J 1 Reply Last reply 12 Aug 2018, 15:40
                0
                • D davethedave
                  12 Aug 2018, 15:24

                  @JonB said in QT SQL query loop?:

                  query.exec("SELECT * FROM users");

                  from the first:
                  http://prntscr.com/khp72u

                  if i append
                  .toUtf8().constData() then the below
                  http://prntscr.com/khp79y

                  from the second:
                  http://prntscr.com/khp72u

                  if i append
                  .toUtf8().constData() then the below
                  http://prntscr.com/khp7tw

                  Also there is only 3 rows now

                  J Offline
                  J Offline
                  JonB
                  wrote on 12 Aug 2018, 15:40 last edited by JonB 8 Dec 2018, 15:42
                  #8

                  @davethedave
                  Firstly, I don't work in C++ so I don't test the code for the right way to pass the output string to printf(). So your corrections are good. [BTW you'd be better off not using printf() and using Qt/C++ functions, then you wouldn't have all that toUtf8().constData() stuff, but that's another matter.]

                  As you can see you have 3 rows matching the query, you tell us you have 5, or you had 5 and now you have 3. That's worrying when you're asking for help. Your Username value appears to be 1 from the output. That is a strange username. I don't know what your problem is. The original code should correctly print out whatever is in the Username column of each row encountered (note that since you didn't put a \n in it will all come on one line, which isn't great, I do trust that is not what this is all about.)

                  1 Reply Last reply
                  1

                  1/8

                  11 Aug 2018, 17:29

                  • Login

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