I want to output int instead of a string
-
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]))
-
@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.
-
@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?
-
@LT-K101
Sometimes you saymale
is printed. Now you saynothing 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.
-
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.
-
@artwaw
The OP is (we think) printingstr( SMale_sql[0][0])
. That is the first column of the first row returned from the query. It might be theint
he says it is, or it might be a stringmale
, or it might be "nothing/empty". But I really don't think it can be all 3 of those at the same time.... -
@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]))
-
@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? -
@LT-K101
Just the sort of thing I thought. So you were quite wrong to say originallyMy 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 returnmale
(orfemale
, but yourWHERE
prevents that) , just as you show.I will leave it to you to think what you actually want from the result set.
-
@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.
-
@LT-K101
Excellent, and correct. And which column contains the number you are interested in? Once you have answered that, how shouldSmales_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?
-
@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
-
@Christian-Ehrlicher I'm really Sorry for disturbing the forum.
-
@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.