N
N
newbie632021-07-14 16:22:25
Python
newbie63, 2021-07-14 16:22:25

How to add a new ORDER BY from sqlite3 to the bot?

Using the /top command, the bot displays the 5 users with the largest numbers:
username1: username1
username2: username2
username3: username3
username4: username4
username5: username5

@dp.message_handler(commands=['top'])
async def top(message: types.Message):
    conn = sqlite3.connect('db.db')
    with conn:
        cur = conn.cursor()    
        cur.execute("SELECT * FROM users ORDER BY money DESC LIMIT 5")
        data = cur.fetchall()
        text = '\n'.join((f'{row[1]}: {row[5]} - монеты' for row in data))
        await message.reply(text)

How to add numbering there:
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 PS In the database there is an id column with user numbering
Then you can display five numbers: cur.execute("SELECT id FROM users ORDER BY id ASC LIMIT 5"), but I don't understand how to add this

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