M
M
Mem13882021-07-07 07:49:30
Python
Mem1388, 2021-07-07 07:49:30

How to separately display each value from the database?

There is a table with columns:
Id ║ UserId ║ DateAdded
If I display all the values ​​using SELECT *, I will get tuples:
(1, 524914064653025280, 2021-07-07)
But I need to make the bot display the data in this format:
60e530e178cee993726546.png
Please, tell me how this can be implemented. Available code:

async def sqlselectall(ctx, arg):
    try:
        result = ' ║ '.join(name[1] for name in cur.execute(f"PRAGMA table_info({arg})"))
        result2 = ' ║ '.join(f'{val}' for val in cur.execute(f"SELECT * FROM {arg}"))
        emb = discord.Embed(
            title = f'{result}',
            description = f'{result2}',
            color = 0xdb7dbc
        )
        await ctx.send(embed = emb)
    except Exception as e:
        emb = discord.Embed(description = f'**{e}**', color = 0xff0000)
        await ctx.send(embed = emb)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-07-07
@Mem1388

The result of executing select in the cursor, like fetchall(), returns not just a tuple, but a nested tuple, i.e.

((ряд1значение1, ряд1значение2, ряд1значение3... ), (ряд2значение1, ряд2значение2, ряд2значение3... ), ...)

accordingly, you need to additionally add the values ​​of the series into a string, and then the strings of the series into a string.
result2 = '\n'.join(' ║ '.join(f'{val}' for val in row) for row in cur.execute(f"SELECT * FROM {arg}"))

And if you want it to be aligned, then you can first read the data, then calculate the longest value in each column, and then output it, formatting it to the largest.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question