#๐Ÿ”’ table formatting

9 messages ยท Page 1 of 1 (latest)

red light
#

I have a table I am calling from a database that is printing like so

[
  [
    "none",
    "192.168.1.23",
    28960, "Solids Test Server",
    151, "none",
    "Soliderror"
  ]
]

I want it to print like so

{servers: [
    {
      id:"none",
      ip:"192.168.1.23",
      port:28960, 
      hostname:"Solids Test Server",
      protocal:151, 
      date:"none",
      hidden:"Soliderror"
    }
  ]
}

I want it to show the entire table with the name of the table

frozen jungleBOT
#

@red light

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

red light
#

How I make the database (idk if this is 100% right for what I want )

async def add_server(identifier,ip,port,hostname,protocal,date,user):
    new_tabel = """ CREATE TABLE IF NOT EXISTS servers (
                                        identifier text,
                                        ip text,
                                        port int,
                                        hostname text,
                                        protocal int,
                                        date int,
                                        hidden int
                                    ); """
    
    async with aiosqlite.connect('master_server.db') as db:
        async with db.cursor():
            await db.execute(new_tabel)
            print("Server Tabel Made")
    async with aiosqlite.connect('master_server.db') as db:
        await db.execute("INSERT INTO servers(identifier,ip,port,hostname,protocal,date,hidden) VALUES(?,?,?,?,?,?,?)",(identifier,ip,port,hostname,protocal,date,user))
        await db.commit()
        print(f" New Server Added To Master:\n Host: {ip}:{port}\n Name: {hostname}\n User: {user}")
#

I am calling it from flask like so

@app.route('/getservers')
async def get_servers_from_master():
    data = await get_server()

    return jsonify(data)


async def get_server():
    async with aiosqlite.connect('master_server.db') as db:
        servers = await db.execute('SELECT * FROM servers')
        info = await servers.fetchall()
        all_servers = []
        return info
wintry gull
#

instead of using SELECT * FROM .. list your columns explicitly. and then use a combination of zip and dict, to create a dictionary for each returned row.

#

!e a reduced example

columns = ['id', 'ip', 'port']
info = [["none", "127.0.0.1", 12345]]  # from select query
info = [dict(zip(columns, row)) for row in info]
print(info)
frozen jungleBOT
#

@wintry gull :white_check_mark: Your 3.12 eval job has completed with return code 0.

[{'id': 'none', 'ip': '127.0.0.1', 'port': 12345}]
frozen jungleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.