H
H
hobilog2021-07-17 05:26:30
Python
hobilog, 2021-07-17 05:26:30

How to get and send all the strings in the Telegram bot that include one of the variables in the database?

I need to extract the name from a string, for example: "s Miha", and use it to find all the lines in the database where this name is, and accordingly send it all in a message to the user.

I tried to write this code:

@bot.message_handler(func=lambda m: True)
def show_oper(message):
    if message.text.startswith("s"):
        mess_oper_name = message.text.split()[1]
        with sqlite3.connect('database.db') as db:
            cursor = db.cursor()
            result = cursor.execute('SELECT * FROM opers_list WHERE oper = ?', [mess_oper_name]).fetchall()
        bot.reply_to(message, '\n'.join(result))

however errors in the console get out and nothing is sent.

Is there any other way to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-07-17
@o5a

result contains a nested list with all the data fields of the table. Something like this: [
(row1value1, row1value2, ...), (row2value1, row2value2, ...), ...
]
. ]
then you can already do '\n'.join()
If you still don't understand, here's an example from recent answers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question