W
W
WolfInChains2020-05-24 12:39:48
Python
WolfInChains, 2020-05-24 12:39:48

How to compare user id with id in sqlite database?

This is how I read, write down and delete IDs from the database.

def select_with_fetchall():
    cmd = "SELECT user_id  FROM banlist"
    c.execute(cmd)
    result = c.fetchall()
    print(result)
def insert():
    cmd = "INSERT INTO banlist(user_id) VALUES (%d)" % (event.obj.reply_message['from_id'])
    c.execute(cmd)
    conn.commit()
    print("Пользователь добавлен в черный список")
def delete():
    cmd = "DELETE FROM banlist WHERE user_id=%d" % event.obj.reply_message['from_id']
    c.execute(cmd)
    conn.commit()
    print("Пользователь удален из черного списка")

This is how I write and delete with bot commands
if event.object.text == "Бан" or event.object.text == "бан":
    if event.obj.from_id in get_admins(event.obj.peer_id):
        insert()
        kick_user_fwd()
if event.object.text == "Анбан" or event.object.text == "анбан":
    if event.obj.from_id in get_admins(event.obj.peer_id):
        delete()
        send_msg(event.obj.peer_id, "Пользователь разбанен")


But I just can't figure out how to compare the user ID with the ID in the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2020-05-24
@WolfInChains

well as an example:

def is_user_banned(user_id: int) -> bool:
    cmd = "select count(user_id) from banlist where user_id = %d" % (user_id)
    c.execute(cmd)
    result = c.fetchone()[0]
    return result > 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question