Discord.py上的SQLite 3排序问题



所以我尝试制作一个Leveling系统,它可以工作,但排行榜不能正常工作,它随机排序SQLite内容,尽管我已经放了ORDER BY lvl also,我尝试过ORDER BY txp(txp是总xp(,这是我写的代码

@commands.command()
async def top10(self, ctx):
db = sqlite3.connect('main.db')
cursor = db.cursor()
cursor.execute(f"SELECT user_id, lvl, txp from levels WHERE guild_id = {ctx.guild.id} ORDER BY txp DESC LIMIT 5 ")
result = cursor.fetchall()
embed = discord.Embed(title="Leaderboards", colour=discord.Colour(0x6790a7))
for i, x in enumerate(result, 1):
embed.add_field(name=f"#{i}", value=f"<@{str(x[0])}> on Level {str(x[1])} with {str(x[2])} Total XP", inline=False)
await ctx.send(embed=embed)
print(result)
cursor.close()
db.close()

这是的结果

[('560578285747306538', '5', '830'), ('441240050861211648', '8', '548'), ('321191503487827970', '4', '548'), ('457159518254792714', '0', '4'), ('448779471810461706', '1', '36')]``` when I print the db

这就是排行榜的样子排行榜看起来像

我猜txp的数据类型是TEXT,所以结果按字母顺序排序
您必须将其转换为整数:

cursor.execute(f"SELECT user_id, lvl, txp from levels WHERE guild_id = {ctx.guild.id} ORDER BY txp + 0 DESC LIMIT 5")

CCD_ 5隐式地将CCD_ 6转换为整数。

最新更新