V
V
Viktor????‍♂️2021-01-02 04:56:38
Python
Viktor????‍♂️, 2021-01-02 04:56:38

How to display data from the database in a telegram bot message (aiogram)?

I have a database, it has a "pnb" column (it stores data about the player's balance)
61d1064a4e689111857150.png

How can I make the bot insert the value of this column into the message?

@dp.message_handler(commands = "spider")
async def start(message: types.Message):
    await message.bot.send_sticker(message.from_user.id, 'CAACAgIAAxkBAAEDl79h0AJfaj1elvgQiZxxI6PsUDUz5gACGAADfoTDCO9Dmo04A4frIwQ')
    await message.bot.send_message(message.from_user.id, ' 0 уровень\n (здесь нужно вставить значение из столбца "pnb") монет')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Chegevara_kyc, 2021-01-02
@klevich

db_name and User_data - database name
user_id= user id

@dp.message_handler(commands = "spider")
async def start(message: types.Message):
    await message.bot.send_sticker(message.from_user.id, 'CAACAgIAAxkBAAEDl79h0AJfaj1elvgQiZxxI6PsUDUz5gACGAADfoTDCO9Dmo04A4frIwQ')
    con = sqlite3.connect(db_name)
    c = con.cursor()
    select_sql="SELECT pnb FROM User_data WHERE user_id = ?" 
    # тут pnb -то что ищем, user_id - признак
    # знак вопроса не трогай
    c.execute(select_sql, (user_id,))
    results = c.fetchall()
    for row in results:

        balance= str(row[0])

    await message.bot.send_message(message.from_user.id, ' 0 уровень','\n',balance ,' монет')

In general, everything here is just like
https://pythonru.com/osnovy/sqlite-v-python
https://www.severcart.ru/blog/all/python_sqlite3/

K
kapp1, 2021-01-02
@kapp1

1. Asynchronously receive data from the database, this can be put into a separate module or into a separate function (immediately make the function return text or a number.
2. You substitute the variable (or function, depending on how you received it) in the sent text, format it as
3. You send to the client .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question