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.5k 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.
  • 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 Online
                      Christian EhrlicherC Online
                      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
                              • L LT-K101

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

                                Christian EhrlicherC Online
                                Christian EhrlicherC Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #21

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

                                Sorry for disturbing the forum.

                                That's not the point. The point is that people who ask should really try to understand what they're asking and doing instead getting every little step done by someone else.

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

                                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.

                                  Pl45m4P Offline
                                  Pl45m4P Offline
                                  Pl45m4
                                  wrote on last edited by Pl45m4
                                  #22

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

                                  i used Smales_sql [0][2] and it worked.

                                  How surprising :)

                                  If you knew your int is in column 3, why do you wrote [0][0]? This will always access the first result, which is always a string, when executing your query.

                                  Btw: There is a section on this website, dedicated to PyQt questions :)

                                  • https://forum.qt.io/category/58/qt-for-python

                                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                  ~E. W. Dijkstra

                                  1 Reply Last reply
                                  0

                                  • Login

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