I have the following function:
def get_some_field(grouping_type_id):
checkConnection() # establishes connection if none
sql = "SELECT name FROM table_foo WHERE id=%d" % grouping_type_id
results = conn.execute(sql)
data = results.fetchone()
return str(data['name']) if ((data) and ('name' in data)) else ''
If the database table is empty, I expect the function to return an empty string however, None is being returned instead.
Can anyone explain this behaviour?
This also returns
'None':The problem is that
(data) and ('name' in data)is truish, becausedatais not empty, and has'name'key set (although set toNone, you do not check it).Change this line:
into this line: