简体   繁体   中英

How can I make the multi-line list from SQLite table in Discord on Python?

I am trying to make bot to send values from SQLite table as a list. Here is table called Employees:

emp_id || emp_name || emp_position

emp_1 || Jane || emp_pos1

emp_2 || Jake || emp_pos2

Here is the function for my command to show the emp list:

@bot.command()
async def EmpTest(ctx):

    with conn:
        cursor.execute("SELECT emp_name, emp_position, emp_id FROM Employees")
        emp_list = cursor.fetchall()

    for j in range(0):
        column = []
        for i in range(0):
            column += emp_list
        column += "\n"
        emp_list += column
    for column in emp_list:
       for item in column:
           print(item, end=" ")
       print()
    await ctx.send(emp_list)

It works perfectly fine when it prints in console:

Jane emp_pos1 emp_1
Jake emp_pos2 emp_2

But that's what Discord gave output after ctx.send(emp_list): [('Jane', 'emp_pos1', emp_1), ('Jake', 'emp_pos2', emp_2)] How can I make it to be send in Discord like in console?

When you're getting a data from SQLite , you get a tuple type data. In this case, you get a list of tuples.

So at first, you need to get each one of the items of the list. You can do that with a for loop .

for emp_info in emp_list:
    print(emp_info)

Simply, you printed 2 employees' information.

Then, you should delete the parentheses. There are several ways to do that. I'll show you one of the ways to do it.

You can use string.join() function. You can do this:

for emp_info in emp_list:
    print(', '.join(emp_info))

Then you can edit this to your code like this:

for emp_info in emp_list:
    await ctx.send(', '.join(emp_info))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM