So I have this bit of code that selects every row in the column userID, puts the returned value in a dictionary then prints it.
whitelist = []
with db.cursor() as cursor:
whitelist_get = "SELECT userID FROM whitelist;"
cursor.execute(whitelist_get)
whitelist = str(cursor.fetchall())
print(whitelist)
And this bit of code checks if the author ID from discord is in that whitelist list.
async def _function(ctx):
global whitelist
if ctx.message.author.id in whitelist:
print("access granted")
else:
print("access not granted")
However, when running the script, the code goes over global whitelist then it does not procced in the if statement nor the else statement. It just does nothing. It doesn't print anything either. Am I doing anything wrong?
EDIT: My author ID is in the MySQL table and it should print access granted but it doesn't print anything at all.
EDIT2: Solved by chaning if ctx.message.author.id in whitelist: to if str(ctx.message.author.id) in whitelist:
The fetchall method returns a sequence of tuples, so you should unpack the tuples to extract the first items first. For efficient lookups, you can make whitelist a set instead:
whitelist = {id for (id,) in cursor.fetchall()}