A
A
ARKRAFTER2020-12-20 11:00:36
SQLite
ARKRAFTER, 2020-12-20 11:00:36

Constantly else although it should beat True how to fix it?

@client.command()
async def addblack(ctx,userid):
  owner = 634060873731670018
  author = ctx.message.author
  if author.id == owner:
    sql.execute(f'SELECT userid FROM badusers')
    if sql.fetchone() is None:
      sql.execute(f'INSERT INTO badusers VALUES (?) ', ({userid}))
      db.commit()
      await ctx.send('готово!')
    else:
      await ctx.send('он уже есть в бд!')
  else:
    await ctx.send('вы не имеете достаточного права чтоб добавлять его как крашера!')

Constantly it is already in the database! although it's not there

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2020-12-20
@ARKRAFTER

Because you are using SQL incorrectly. The query selects all records from badusers, not just a specific user_id. As far as I can see, it is supposed to check if there is a user and enter it.

sql.execute(f'SELECT userid FROM badusers WHERE user_id = ?', (user_id, ))

And it's better to add a unique index / primary key on this user_id and you don't have to do checks before adding it.
Secondly, incorrect syntax in execute for INSERT, curly braces are not needed there, just
sql.execute(f'INSERT INTO badusers VALUES (?) ', (userid, ))

A
Alan Gibizov, 2020-12-20
@phaggi

See what sql.fetchone()returns. There is clearly something there, but not None.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question