Home>

Hello everyone! Tell me how to do user input validation through a SQLite database. It is necessary for the user to enter a number that is in the database on a specific line and perform certain actions. Let me show you with 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: # I got stuck here, what condition should I put?
                bot.send_message (message.chat.id, 'This is the number you need, you sent the number that is in the table')
        if (message.text.isdigit ()) and (check is None):
                bot.send_message (message.chat.id, 'You sent a number that is not in the table')
        else:
            bot.send_message (message.chat.id, 'You sent anything but a number.')
    except Exception as e:
        bot.send_message (message.chat.id, 'Error')

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

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

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

it is obvious that you need to check what fetchone returns, if the number is not in the table, then an empty tuple will be returned -False, if the tuple is not empty -True

Clark Devlin2021-11-21 15:31:21

@ –°lark Devlin, i.e. if (message.text.isdigit ()) and check is True? It doesn't work yet, if it's not difficult to add an example to the answer, I can mark it at the same time.

ProNoob2021-11-21 16:16:06
  • Answer # 1

    Firstly, if you need to check the value of a specific variable, then you should not retrieve the entire row from the database, but rather retrieve the specific value to be checked, for example, instead of:

    check= cursor.execute ('SELECT * FROM Table_table WHERE line_number=?', (line_number,)). fetchone ()
    

    is best done:

    check= cursor.execute ('SELECT column_name FROM Table_table WHERE line_number=?', (line_number,)). fetchone ()
    

    If user input is available by message.text, and the data types entered by the user coincide with the type obtained from the database, then you can try like this:

    if (message.text.isdigit ()) and message.text== check [0]:
    

    Those. would it be correct to enter line_number in the conditional 'column_name'? And if the user input matches the number deduced from the table, then this particular condition is met? I try, but so far it does not work, it goes into an error.

    ProNoob2021-11-21 15:54:35

    When I enter a number that is not in the table and it is assumed that the second condition is fulfilled with None. Swears at checking the first if with message.text== check [0]. Error: 'NoneType' object is not subscriptable

    ProNoob2021-11-21 16:02:31

    Probably the result of a query in the database returns None and it is necessary to implement the check of the result of a query to the database for the returned data type.

    Andrew2021-11-21 16:59:46