D
D
Dmitry Kasatkin2022-04-09 18:09:12
Python
Dmitry Kasatkin, 2022-04-09 18:09:12

How to pass the value res[0] to the next step_2 to create a SQL query?

Here is an example of a row from the database:
ID - "155" street - "Gagarina" house - "36/3" komm - "Facade house" street_str - "Gagarina"

When entering the address, I need to extract the record ID and in the second step make a query to SQLite . But instead of the ID, the originally entered address is returned to me
Query: "Gagarina 36/3" =>
Step_2: "Enter a comment"
Write a comment to the temp variable
Comment: "Comment"
Query to the database: "Unify the old comment and the new one by record ID"

But for now the program does not go to step_2 and does not display errors.

@bot.message_handler(commands=['add'])
def handle_text (message):
    bot.send_message(message.chat.id, f"{message.from_user.first_name}, введите адрес")
    @bot.message_handler(content_types=['text'])
    def handle_text(message):
        db = sqlite3.connect('db.sqlite')
        cursor = db.cursor()
        txt = message.text
        zap = txt.rsplit(' ', 1)
        street = zap[0]
        house = zap[1]
        street_str = zap[0].lower()
        cursor.execute("SELECT * FROM tkd WHERE street_str LIKE '%' || ? || '%' AND  house = ?", (street_str, house))
        res = cursor.fetchone()
        if res:
            res[0]
            res[3]
        else:
            bot.send_message(message.chat.id, 'Проверьте адрес')
        bot.register_next_step_handler(message, step_2)


def step_2(message):
    id = res[0]
    temp_komm = res[3]
    bot.send_message(message.chat.id, 'Введите комментарий')
    new_komm = message.txt
    db = sqlite3.connect('db.sqlite')
    cursor = db.cursor()
    cursor.execute(f"UPDATE tkd SET komm = {temp_komm}\n{new_komm} WHERE id == {id}")
    cursor.close()
    db.commit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Михаил Кростелев, 2022-04-09
@TKDBOT

Выносите хендлер из функции.
Если вы хотите через register_next_step_handler передать параметры какие-либо, передавайте их третьим и далее параметром. Пример можете посмотреть в ответах тут.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question