Answer the question
In order to leave comments, you need to log in
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)
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
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 ,' монет')
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 questionAsk a Question
731 491 924 answers to any question