P
P
ProooNoob2021-11-23 13:11:27
Python
ProooNoob, 2021-11-23 13:11:27

How to validate user input?

I welcome everyone! Please tell me how to do user input validation through SQLite database. It is necessary that the user enters a number that is in the database in a certain line and certain actions are performed. Let me show you a code example:

def onetwofre(message):
    try:
        line_number = message.text
        check = cursor.execute('SELECT * FROM Table_table WHERE line_number=?', (line_number, )).fetchone()
        if (message.text.isdigit()) and : # Вот тут застрял, какое условие необходимо поставить?
                bot.send_message(message.chat.id, 'Именно этот номер и нужен, вы отправили число, которое есть в таблице')
        if (message.text.isdigit()) and (check is None):
                bot.send_message(message.chat.id, 'Вы отправили число, которого нет в таблице')
        else:
            bot.send_message(message.chat.id, 'Вы отправили что угодно, но не число.')
    except Exception as e:
        bot.send_message(message.chat.id, 'Ошибка')


If the user enters a number that is in the table, then fetchone returns the entire row. Is it possible to somehow make it return True or if some value is returned, then the first condition would be fulfilled?

If the user enters a number that is not in the table, then fetchone returns None, which matches the condition and the loop fires.

If the user enters text or something else, the third condition is triggered.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-11-23
@ProooNoob

if (message.text.isdigit()) and check:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question