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. I want to output int instead of a string
Qt 6.11 is out! See what's new in the release blog

I want to output int instead of a string

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 6 Posters 3.2k Views 2 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.
  • L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #1

    My sql statement returns an int value but when I want to display in a Qlabel , It returns a string. Please below is my code. Any help would be appreciated. Thanks.

    senior_staff_females = ''' slq statement '''
    SMale_sql = cur.execute(senior_staff_females).fetchall()
    self.ui.label_533.setText(str(SMale_sql[0][0]))
    print(str(sql[0][0]))
    
    jsulmJ 1 Reply Last reply
    0
    • L LT-K101

      My sql statement returns an int value but when I want to display in a Qlabel , It returns a string. Please below is my code. Any help would be appreciated. Thanks.

      senior_staff_females = ''' slq statement '''
      SMale_sql = cur.execute(senior_staff_females).fetchall()
      self.ui.label_533.setText(str(SMale_sql[0][0]))
      print(str(sql[0][0]))
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @LT-K101 I don't understand the problem: QLabel shows strings and images.
      So, what is the problem?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      1
      • jsulmJ jsulm

        @LT-K101 I don't understand the problem: QLabel shows strings and images.
        So, what is the problem?

        L Offline
        L Offline
        LT-K101
        wrote on last edited by
        #3

        @jsulm Sorry, I want to display total of males who are junior officers from my database table. Now when I run this query in sql it works perfectly with an int value as output. But when I use same sql statement in the above code it return a string say 'male' instead of the total value in a string format.

        jsulmJ 1 Reply Last reply
        0
        • L LT-K101

          @jsulm Sorry, I want to display total of males who are junior officers from my database table. Now when I run this query in sql it works perfectly with an int value as output. But when I use same sql statement in the above code it return a string say 'male' instead of the total value in a string format.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LT-K101 I don't see how QLabel could influence what a SQL query returns.
          Also, you are not printing same thing in your code:

          self.ui.label_533.setText(str(SMale_sql[0][0]))
          print(str(sql[0][0]))
          

          SMale_sql[0][0] is not the same thing as sql[0][0], right?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          0
          • jsulmJ jsulm

            @LT-K101 I don't see how QLabel could influence what a SQL query returns.
            Also, you are not printing same thing in your code:

            self.ui.label_533.setText(str(SMale_sql[0][0]))
            print(str(sql[0][0]))
            

            SMale_sql[0][0] is not the same thing as sql[0][0], right?

            L Offline
            L Offline
            LT-K101
            wrote on last edited by LT-K101
            #5

            @jsulm Sorry, I have reverted it from

            print(str(sql[0][0]))  
            

            to

            print(str( SMale_sql[0][0]))
            

            but nothing happens.

            JonBJ 1 Reply Last reply
            0
            • L LT-K101

              @jsulm Sorry, I have reverted it from

              print(str(sql[0][0]))  
              

              to

              print(str( SMale_sql[0][0]))
              

              but nothing happens.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @LT-K101
              Sometimes you say male is printed. Now you say nothing happens.

              Please think if you want to ask us a question. If you do not clearly state what happens how can other people answer? Show the code as it is now (not as it was earlier which you now say was wrong), state clearly what it does or does not output where, and state what you would like/think it should do. We are not mind-readers.

              L 1 Reply Last reply
              2
              • artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #7

                You didn't state which kind of connector you use however looking at the generic example here https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html I'd say you just need to casts the result to int? The query return value can be dict, tuple or raw, so it is up to you to decide what type you want.

                For more information please re-read.

                Kind Regards,
                Artur

                JonBJ 1 Reply Last reply
                0
                • artwawA artwaw

                  You didn't state which kind of connector you use however looking at the generic example here https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html I'd say you just need to casts the result to int? The query return value can be dict, tuple or raw, so it is up to you to decide what type you want.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @artwaw
                  The OP is (we think) printing str( SMale_sql[0][0]). That is the first column of the first row returned from the query. It might be the int he says it is, or it might be a string male, or it might be "nothing/empty". But I really don't think it can be all 3 of those at the same time....

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @LT-K101
                    Sometimes you say male is printed. Now you say nothing happens.

                    Please think if you want to ask us a question. If you do not clearly state what happens how can other people answer? Show the code as it is now (not as it was earlier which you now say was wrong), state clearly what it does or does not output where, and state what you would like/think it should do. We are not mind-readers.

                    L Offline
                    L Offline
                    LT-K101
                    wrote on last edited by
                    #9

                    @JonB Alright, please below is my block of code

                    senior_staff_males = '''  
                                     SELECT staff_gender, staff_grade,
                                     COUNT(*) AS total
                                     FROM permanent_staff
                                     WHERE staff_gender = 'Male' AND staff_grade = 'Senior Officer'  
                                          '''
                    Smales_sql = cur.execute(senior_staff_males).fetchall()
                    self.ui.label_533.setText(str(Smales_sql [0][0]))
                    print(str(Smales_sql [0][0]))
                    
                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • L LT-K101

                      @JonB Alright, please below is my block of code

                      senior_staff_males = '''  
                                       SELECT staff_gender, staff_grade,
                                       COUNT(*) AS total
                                       FROM permanent_staff
                                       WHERE staff_gender = 'Male' AND staff_grade = 'Senior Officer'  
                                            '''
                      Smales_sql = cur.execute(senior_staff_males).fetchall()
                      self.ui.label_533.setText(str(Smales_sql [0][0]))
                      print(str(Smales_sql [0][0]))
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @LT-K101 said in I want to output int instead of a string:

                      staff_gender

                      So, this one is a string.
                      Why do you expect it to be an int?
                      How should it become an int?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • L LT-K101

                        @JonB Alright, please below is my block of code

                        senior_staff_males = '''  
                                         SELECT staff_gender, staff_grade,
                                         COUNT(*) AS total
                                         FROM permanent_staff
                                         WHERE staff_gender = 'Male' AND staff_grade = 'Senior Officer'  
                                              '''
                        Smales_sql = cur.execute(senior_staff_males).fetchall()
                        self.ui.label_533.setText(str(Smales_sql [0][0]))
                        print(str(Smales_sql [0][0]))
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @LT-K101
                        Just the sort of thing I thought. So you were quite wrong to say originally

                        My sql statement returns an int value

                        . Now when I run this query in sql it works perfectly with an int value as output.

                        Both of these were not true for that query, were they? It is not helpful for you to have claimed what you do above.

                        Since the first column returned is staff_gender, str(Smales_sql [0][0]) will presumably return male (or female, but your WHERE prevents that) , just as you show.

                        I will leave it to you to think what you actually want from the result set.

                        L 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @LT-K101
                          Just the sort of thing I thought. So you were quite wrong to say originally

                          My sql statement returns an int value

                          . Now when I run this query in sql it works perfectly with an int value as output.

                          Both of these were not true for that query, were they? It is not helpful for you to have claimed what you do above.

                          Since the first column returned is staff_gender, str(Smales_sql [0][0]) will presumably return male (or female, but your WHERE prevents that) , just as you show.

                          I will leave it to you to think what you actually want from the result set.

                          L Offline
                          L Offline
                          LT-K101
                          wrote on last edited by
                          #12

                          @JonB This is really confusing because when i run the sql statement in sql it returns total value count which is true, so I was thinking I could convert that to a string in my qt script

                          JonBJ 1 Reply Last reply
                          0
                          • L LT-K101

                            @JonB This is really confusing because when i run the sql statement in sql it returns total value count which is true, so I was thinking I could convert that to a string in my qt script

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @LT-K101
                            But that is not what the statement (just) returns, is it?

                            Please try to understand the minimal amount. You have SELECT staff_gender, staff_grade, COUNT(*) AS total. How may columns does that return?

                            L 1 Reply Last reply
                            2
                            • JonBJ JonB

                              @LT-K101
                              But that is not what the statement (just) returns, is it?

                              Please try to understand the minimal amount. You have SELECT staff_gender, staff_grade, COUNT(*) AS total. How may columns does that return?

                              L Offline
                              L Offline
                              LT-K101
                              wrote on last edited by LT-K101
                              #14

                              @JonB said in I want to output int instead of a string:

                              SELECT staff_gender, staff_grade, COUNT(*) AS total

                              It returns three columns namely staff_gender,staff_grade, total respectively when i run in sql with thier values. I get string for the first two columns and an int for the third column but I only need the third value which is the total.

                              JonBJ 1 Reply Last reply
                              0
                              • L LT-K101

                                @JonB said in I want to output int instead of a string:

                                SELECT staff_gender, staff_grade, COUNT(*) AS total

                                It returns three columns namely staff_gender,staff_grade, total respectively when i run in sql with thier values. I get string for the first two columns and an int for the third column but I only need the third value which is the total.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

                                @LT-K101
                                Excellent, and correct. And which column contains the number you are interested in? Once you have answered that, how should Smales_sql [0][0] be that column? What should it be for the column you are interested in?

                                You just edited to add:

                                I get string for the first two columns and an int for the third column but I only need the third value which is the total.

                                Again, excellent and true. So if you tell me "I only need the third value", what have you done/what should you do in your code to access that 3rd column?

                                Christian EhrlicherC L 2 Replies Last reply
                                3
                                • JonBJ JonB

                                  @LT-K101
                                  Excellent, and correct. And which column contains the number you are interested in? Once you have answered that, how should Smales_sql [0][0] be that column? What should it be for the column you are interested in?

                                  You just edited to add:

                                  I get string for the first two columns and an int for the third column but I only need the third value which is the total.

                                  Again, excellent and true. So if you tell me "I only need the third value", what have you done/what should you do in your code to access that 3rd column?

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @JonB said in I want to output int instead of a string:

                                  Again, excellent and true. So if you tell me "I only need the third value", what have you done/what should you do in your code to access that 3rd column?

                                  But teacher it's Friday and very late already - can't you do this for me?

                                  And btw: I though this is a forum about Qt, not python

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  L 1 Reply Last reply
                                  2
                                  • JonBJ JonB

                                    @LT-K101
                                    Excellent, and correct. And which column contains the number you are interested in? Once you have answered that, how should Smales_sql [0][0] be that column? What should it be for the column you are interested in?

                                    You just edited to add:

                                    I get string for the first two columns and an int for the third column but I only need the third value which is the total.

                                    Again, excellent and true. So if you tell me "I only need the third value", what have you done/what should you do in your code to access that 3rd column?

                                    L Offline
                                    L Offline
                                    LT-K101
                                    wrote on last edited by LT-K101
                                    #17

                                    @JonB Thanks a lot I really appreciate your guidance. i used Smales_sql [0][2] and it worked.

                                    JonBJ Pl45m4P 2 Replies Last reply
                                    1
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @JonB said in I want to output int instead of a string:

                                      Again, excellent and true. So if you tell me "I only need the third value", what have you done/what should you do in your code to access that 3rd column?

                                      But teacher it's Friday and very late already - can't you do this for me?

                                      And btw: I though this is a forum about Qt, not python

                                      L Offline
                                      L Offline
                                      LT-K101
                                      wrote on last edited by
                                      #18

                                      @Christian-Ehrlicher I'm really Sorry for disturbing the forum.

                                      Christian EhrlicherC 1 Reply Last reply
                                      0
                                      • L LT-K101

                                        @JonB Thanks a lot I really appreciate your guidance. i used Smales_sql [0][2] and it worked.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #19

                                        @LT-K101 said in I want to output int instead of a string:

                                        i used Smales_sql [0][2]

                                        That's much better :) You can see now why you needed to use that, and it's best that you came to that conclusion yourself because then you understand what is going on.

                                        L 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @LT-K101 said in I want to output int instead of a string:

                                          i used Smales_sql [0][2]

                                          That's much better :) You can see now why you needed to use that, and it's best that you came to that conclusion yourself because then you understand what is going on.

                                          L Offline
                                          L Offline
                                          LT-K101
                                          wrote on last edited by
                                          #20

                                          @JonB Yes Yes You guys are the best.

                                          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