Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Python failing to check if a value is in a list

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:

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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()}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda