Search code examples
pythontelebot

How do I get the entire list from the database in the bot?


I want to output a text telegram bot. But in my code, all the data (in my case, words, Dictionary.translate, description, Terms.translate) is output only in the console, and in the telegram it outputs data only from the first column. how do I make it so that all columns are displayed on request?

@bot.message_handler(content_types=['text'])
def funcs_mess(message):
    if message.text == 'Переводчик':
        bot.send_message(message.chat.id, 'Напишите сообщение, которое нужно перевести:')
        bot.register_next_step_handler(message, translator_mess)
    elif message.text == 'Дополнить словарь':
        bot.send_message(message.chat.id, 'Напишите 2 сообщения (предложение и его перевод), которые нужно добавить в словарь:')
        bot.register_next_step_handler(message, dictionaryAdd_mess1)
    elif message.text == 'Словарь':
        bot.send_message(message.chat.id, 'Вывожу словарь:')
        #bot.register_next_step_handler(message, dictionary_mess)
        with closing(connect_to_db()) as conn:
            with conn.cursor() as cursor:
                cursor.execute('select words, Dictionary.translate, description, Terms.translate from Dictionary inner join Terms on Dictionary.id = Terms.words_id;')
               for row in cursor:
                    print(row)
                    bot.send_message(message.chat.id, row)
        bot.reply_to(message, "Selected to the dictionary.")
    else:
        bot.send_message(message.chat.id, 'Я тебя не понимаю')

Solution

  • You are passing a tuple to the send_message, but it takes a str as the text parameter. See the documentation for send_message

    You first need to format it into a string. Something like this:

    for row in cursor:
        print(row)
        words, dict_translate, description, terms_translate = row
        formatted_row = f"Words: {words}\nDescription: {description}\nTranslates: {dict_translate}\nTerms: {terms_translate}"
        bot.send_message(message.chat.id, formatted_row )