python-3.x 从DB中分离结果,以便在DC中进行审查

qvtsj1bj  于 2023-05-23  发布在  Python
关注(0)|答案(1)|浏览(100)

用户的所有条目都是从数据库中提取的。这些都是一个接一个的不可读的。是否可以将每一行写入一个新行?

@commands.command()
    async def scoredetail(self, ctx):
        async with aiosqlite.connect("members.db") as db:
             async with db.execute(
                 'SELECT ALL Punktestand, Klasse, Zeit FROM Mitglieder WHERE ID = ?', (ctx.author.id,))  as cursor:
                result = await cursor.fetchall()
                
                if result is None:
                    await ctx.send("{member} hat noch keine Punkte!")
                    return
            
                         
                await ctx.send(f"Deine Punkte: {result}.")

DC结果:
Deine Punkte:[(1,'文本','22-05-2023 09:25:04'),(1,'文本','22-05-2023 09:27:09'),(1,'文本','22-05-2023 09:28:25')]。
看看Youtube,谷歌,等等

7uhlpewt

7uhlpewt1#

你可以遍历cursor.fetchall(),因为它返回Iterable[Row],如下所示:

@commands.command()
    async def scoredetail(self, ctx):
        async with aiosqlite.connect("members.db") as db:
             async with db.execute(
                 'SELECT ALL Punktestand, Klasse, Zeit FROM Mitglieder WHERE ID = ?', (ctx.author.id,))  as cursor:
                result = await cursor.fetchall()
                
                if result is None:
                    await ctx.send("{member} hat noch keine Punkte!")
                    return
                else:    
                    finalResult = ''
                    for row in result:
                        finalResult += f'{row}\n'
            
                         
                await ctx.send(f"Deine Punkte: {finalResult}.")

这将在结果中的每一行之间创建一个换行符。
API参考

相关问题