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

QT SQL query loop?

Scheduled Pinned Locked Moved Unsolved Brainstorm
8 Posts 3 Posters 3.2k 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.
  • D Offline
    D Offline
    davethedave
    wrote on last edited by davethedave
    #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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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 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

        JonBJ 1 Reply Last reply
        0
        • D davethedave

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

          and its the only check i could think of

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          0
          • JonBJ JonB

            @davethedave

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

            @JonB
            Prints first entry in the table still.

            JonBJ 1 Reply Last reply
            0
            • D davethedave

              @JonB
              Prints first entry in the table still.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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 last edited by davethedave
                #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

                JonBJ 1 Reply Last reply
                0
                • D davethedave

                  @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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #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

                  • Login

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