If case not working for empty result with sql query python

Zaid Alvi :

So basically I am trying to retrieve values from a database. My if case is that if the query result which is saved in variable data is None then it should print message data not found. If the value is not none then it should print the data. My problem is that I am on purpose giving it a value which is not present in the database which means data should be null. But my if none case is not triggering. It goes directly to else. I have attached pictures down below. Apologies if this question is too basic. enter image description here

data variable with value []

Steve Mapes :

This is because you are testing if the data variable is am instance of the None object which, from your output, it's not it's a list.

So all you need to do is to change your if statement to if not data: which will fix the problem.

Not will pass true for None, False and empty list amongst other things. see Python Truth Value Testing

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23784&siteId=1