[Python] Add Column Names to retrieved data from pymysql [duplicate]

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple SQL command via pymsql to retrieve data

    db = pymysql.connect(
    db=DATABASE,
    passwd=DB_PASSWORD,
    host=DB_HOST,
    user=DB_USER,
    )
    cursor = db.cursor()
    cursor.execute("""
    select flight, aircraft_model
    from flights
    where flights.aircraft_type in ('boeing', 'airbus')
    """)
    res = cursor.fetchall()


    This will return a array of tuples e.g.

    [
    ('tk123', 'b737'),
    ('us123', 'a230')
    ]


    How can I have the first tuple to be the column names e.g.

    [
    ('flight', 'aircraft_model')
    ('tk123', 'b737'),
    ('us123', 'a230')
    ]

    Continue reading...

Compartilhe esta Página