获取多行问题

Issue getting a number of rows

您好,我要从 psycopg2 转到 asyncpg,我在尝试 return asyncpg 中的多行时遇到问题,在本例中为 10。在 psycopg2 中,我将通过以下方式执行此操作:

psycopg2

cursor.execute(f"SELECT user_id,lvl,xp,total_xp FROM levels WHERE guild_id = {str(701547632666525246)} ORDER BY GREATEST(total_xp) DESC")
        res = cursor.fetchmany(10)

asycpg

res = await conn.fetch("SELECT user_id,lvl,xp,total_xp FROM levels WHERE guild_id =  ORDER BY GREATEST(total_xp) DESC", ctx.guild.id)

我有点卡在 asyncpg 部分,我们将不胜感激。

这是我正在处理的内容:

@commands.command()
async def ranking(self, ctx):
    """Displays top 10 Active Members in the server"""
    if ctx.message.channel.name == "commands":
        async with self.bot.pool.acquire() as conn:
            res = await conn.fetch("SELECT user_id,lvl,xp,total_xp FROM levels WHERE guild_id =  ORDER BY GREATEST(total_xp) DESC", ctx.guild.id)
            #0: user_id, 1: lvl
            mess = "Top 10 active members"
            x = 1
            for ele in res:
                user = self.bot.get_user(int(ele[0]))
                mess += "**{}.** {} on **Level {: <3}** with **{: <4} XP**\n\n".format(x,user.name,ele[1], ele[2])
                x+=1
            embed = discord.Embed(title="Leaderboard", description=f"{mess}")
            await ctx.send(embed=embed)
    else:
        await ctx.send("Can't use this command in this channel.")

您可以简单地使用 LIMIT 子句

query = """
    SELECT user_id,lvl, xp, total_xp 
    FROM levels 
    WHERE guild_id =  
    ORDER BY GREATEST(total_xp) DESC
    LIMIT 10
"""
await conn.fetch(query, ctx.guild.id)

它适用于 psycopg2asyncpg