Python 无法检查某个值是否在列表中

Python failing to check if a value is in a list

所以我有这段代码选择 userID 列中的每一行,将返回值放入字典然后打印它。

whitelist = []            

with db.cursor() as cursor:
    whitelist_get = "SELECT userID FROM whitelist;"
    cursor.execute(whitelist_get)
    whitelist = str(cursor.fetchall())
    print(whitelist)

这段代码检查来自 discord 的作者 ID 是否在白名单中。

async def _function(ctx):
    global whitelist
    if ctx.message.author.id in whitelist:
        print("access granted")
    else:
        print("access not granted")

但是,当 运行 脚本时,代码遍历 global whitelist 然后它不会在 if 语句或 else 语句中处理。它什么都不做。它也不打印任何东西。我做错了什么吗?

编辑:我的作者 ID 在 MySQL table 中,它应该打印 access granted 但它根本不打印任何东西。

EDIT2:通过将 if ctx.message.author.id in whitelist: 更改为 if str(ctx.message.author.id) in whitelist:

来解决

fetchall 方法 returns 一个元组序列,因此您应该首先解压缩元组以提取第一个项目。为了高效查找,您可以将白名单设置为一组:

whitelist = {id for (id,) in cursor.fetchall()}