N
N
newbie632021-07-07 13:26:32
Python
newbie63, 2021-07-07 13:26:32

How can a bot display top 5 users from sqlite3?

I have a database with a user id, nickname and number that the bot works with. The bot displays the user's number like this

@dp.message_handler(commands=['number'])
async def number(message: types.Message):
    user_id = message.from_user.id
    first_name = message.from_user.first_name
    username = message.from_user.username
    conn = sqlite3.connect('db.db')
    cur = conn.cursor()
    result = cur.execute("SELECT * FROM users WHERE user_id = ?", (user_id, )).fetchone()
    number = result[5]
    await message.reply(f'Ваше число: {number}')

In the database, I can arrange the numbers so that the first one is the one with the largest number and further in descending order. How to deduce from this the 5 users with the highest numbers. And the bot, when prescribing the /top command, wrote:
1. user nickname1: user
number1 2. user nickname2: user
number2 3. user nickname3: user
number3 4. user nickname4: user
number4 5. user nickname5: user number5

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question