def get_user_data(username: str, columns: list):
result = []
for column in columns:
query = f"SELECT {column} FROM ta_users WHERE username = '{username}'"
cursor.execute(query)
print('cursor.fetchone() from loop = ', cursor.fetchone(),
'type= ', type(cursor.fetchone())) # debug
fetchone = cursor.fetchone()
result.append(fetchone[0])
raises:
result.append(fetchone[0])
TypeError: 'NoneType' object is not subscriptable
the print above returns:
cursor.fetchone() from loop = ('6005441021308034',) type= <class 'NoneType'>
I read why it can return None, but i did not figure out how to work around it.
how to make it work and return list of values after the loop?
CodePudding user response:
You are "emptying" it in the first fetchone()
run, that's why you are getting None
in the following (it looks for the second record, but can't find it. Try this code:
def get_user_data(username: str, columns: list):
result = []
for column in columns:
query = f"SELECT {column} FROM ta_users WHERE username = '{username}'"
cursor.execute(query)
res = cursor.fetchone()
print('cursor.fetchone() from loop = ', res,
'type= ', type(res))) # debug
if res:
result.append(res[0])