W
W
WolfInChains2020-05-25 14:29:12
Python
WolfInChains, 2020-05-25 14:29:12

What is the correct way to do sqlite validation?

After the first varna stops working. I changed a little chek_user_warnand tried using

if chek_user_warn(event.obj.reply_message['from_id']) ==  1:
, then it reached 2, but when a participant was prescribed 3 varns, it was considered as 2. And also how to correctly increase the number of varns in the table by 1, I tried it using traditional methods, it didn’t work, well, or I’m a handyman.
if event.object.text == "Варн" or event.object.text == "варн":
    if event.obj.from_id in get_admins(event.obj.peer_id):
        if not is_user_warned(event.obj.reply_message['from_id']):
            insert_warnlist(event.obj.reply_message['from_id'])
            add_warn(event.obj.reply_message['from_id'])
            send_msg(event.obj.peer_id, "✅Пользователь получил 1/3 варнов")
        else:
            if chek_user_warn(event.obj.reply_message['from_id'], 1):
                add_warn(event.obj.reply_message['from_id'])
                send_msg(event.obj.peer_id, "✅Пользователь получил 2/3 варнов")
            if chek_user_warn(event.obj.reply_message['from_id'], 2):
                add_warn(event.obj.reply_message['from_id'])
                send_msg(event.obj.peer_id, "✅Пользователь получил 3/3 варнов")
                kick_user_fwd()


def is_user_warned(user_id: int) -> bool:
    cmd = "select count(user_id) from warnlist where user_id = %d" % (user_id)
    c.execute(cmd)
    result = c.fetchone()[0]
    print("Проверка айди на наличие в базе")
    return result > 0


def insert_warnlist(user_id: int):
    cmd = "INSERT INTO warnlist(user_id) VALUES (%d)" % (user_id)
    c.execute(cmd)
    conn.commit()
    print("Пользователь добавлен в варнлист")


def delete_warnlist(user_id: int):
    cmd = "DELETE FROM warnlist WHERE user_id=%d" % user_id
    c.execute(cmd)
    conn.commit()
    print("Пользователь удален из варнлиста")


def add_warn(user_id: int):
    cmd = "UPDATE warnlist SET warns = +1 WHERE user_id=%d" % user_id
    c.execute(cmd)
    conn.commit()
    print("Пользователь получил +1 варн")


def chek_user_warn(user_id: int, warns: int):
    cmd = "select count(warns) from warnlist where (user_id = %d, warns = %s)" % (user_id, warns)
    c.execute(cmd)
    result = c.fetchone()[0]
    print("Проверка варнов пользователя")
    return result > 0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WolfInChains, 2020-05-25
@WolfInChains

looked at the example and understood what the error is

A
Alexander, 2020-05-25
@shabelski89

In your case, when calling each of the functions, it is not enough to open a connection to the database.

with sqlite3.connect('my.db') as connection:
    cursor = connection.cursor()
    cursor.execute(cmd)

I already gave an example of interaction here to minimize code duplication.
Next, incomprehensible checks and transformations are used: in the code above, a check is used, if True is returned, then do it, then
def is_user_warned(user_id: int) -> bool:
if not is_user_warned(event.obj.reply_message['from_id']):

it is not necessary to explicitly return return result > 0 , if result is not empty, then it will be True.
return a value that you can then work with, and it doesn't hurt to check for emptiness.
def post_sql_query(sql_query):
    with sqlite3.connect('my.db') as connection:
        cursor = connection.cursor()
        try:
            cursor.execute(sql_query)
        except Error:
            pass
        result = cursor.fetchall()
        return result


def is_user_warned(user_id: int):
    cmd = "select count(user_id) from warnlist where user_id = %d" % (user_id)
    result  = post_sql_query(cmd)
    return result


if not is_user_warned(event.obj.reply_message['from_id']):
    print('добавить')
else:
    print('+ 1/3 варн')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question