I
I
Ivanus2014-03-11 15:03:00
MySQL
Ivanus, 2014-03-11 15:03:00

Python & MySQL | How to compare the received value from the request?

I can not compare the received value in any way.
I make a request like this:

db = MySQLdb.connect(host=**,user=**,passwd=**,db=**)
c = db.cursor()

c.execute("SELECT `status` AS TEXT FROM table ORDER BY id DESC LIMIT 1")
status = [item[0] for item in c.fetchall()]
print status

At the output, I get the following value: "[0L]" - I don’t understand at all how such a value turned out.
In a database, status with type INT(2) has the value = 0, and python outputs it as "[0L]".
Hence the question, how to get the correct value from the database (status) in order to compare it later ( if status == 0 etc.) ?
PS I already tried even the received answer ( "[0L]" ) compare if status == "[0L]" - doesn't work

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivanus, 2014-03-11
@Ivanus

I solved this problem in this way:
after receiving, I translated the variable into a string
And now if status == "0" - it works without problems.
Thanks everyone for the replies.

T
tema_sun, 2014-03-11
@tema_sun

Your status is a list of results. But since there is only one result, then your list contains only one element. The letter L means that your result is "long", but you do not need to write this L when comparing.
When you write if status == "[0L]", you are comparing status to the string "[0L]", not to the list containing element 0L.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question