A
A
Alexey Mikhalev2020-07-20 20:20:43
Python
Alexey Mikhalev, 2020-07-20 20:20:43

Why is the data from the database displayed with extra characters?

Good evening. I am writing a bot. The essence is simple, the user clicks on the desired button, and then the bot edits the message to the text that the code forms by pulling the text from several SQLite database cells. I am using the PyTelegramBotApi library. But the data is displayed in brackets, quotes and a comma at the end. How to fix it? Thanks in advance to everyone for your help.

Output:

('Пример названия',)
----------------------------------
('Тут много текста ...',)

Цена: ('2500000',)

Functions of calls to the database:
def product_name(id ):
    with sqlite3.connect('database.db') as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT `name` FROM `catalog` WHERE `id` = ? ", (id,))
        result = cursor.fetchone()
        return result

def product_about(id ):
    with sqlite3.connect('database.db') as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT `about` FROM `catalog` WHERE `id` = ? ", (id,))
        result = cursor.fetchone()
        return result
 
def product_price(id ):
    with sqlite3.connect('database.db') as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT `price` FROM `catalog` WHERE `id` = ? ", (id,))
        result = cursor.fetchone()
        return result

Button output code:
if call.data == 'bud-1':

        markup = types.InlineKeyboardMarkup(row_width=1)
        item1 = types.InlineKeyboardButton("Назад", callback_data='bud')
        markup.add(item1)

        bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=str(product_name('bud1')) + "\n" + "----------------------------------\n" + str(product_about('bud1')) + "\n" + "\n"+"Цена: " + str(product_price('bud1')), parse_mode='html', reply_markup=markup)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
raitonoberu, 2020-07-20
@mikkhalev

You will convert the tuple to a string:

>>> a = ("foo",)  # кортеж из одного значения
>>> str(a)  # преобразовать в строку
"('foo',)"
>>> a[0]  # получить значение
'foo'

To fix, replace str(product_name('bud1')) with product_name('bud1')[0]

D
Dmitry, 2020-07-20
@LazyTalent

fetchone()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question