getting database column names from query

Discussion in 'Python' started by Jason Nordwick, Aug 16, 2006.

  1. I'm using MySQLdb and can connect and issue queries that return result sets, but I how do I get the column names for those result sets?

    >>> c = MySQLdb.connect(*creds)
    >>> k = c.cursor()
    >>> k.execute("select * from account")

    3L
    >>> k.fetchall()

    ((1L, 'test', -1L), (2L, 'Test', -1L), (3L, 'Test2', -1L))


    -j
     
    Jason Nordwick, Aug 16, 2006
    #1
    1. Advertising

  2. Jason Nordwick

    Simon Forman Guest

    Jason Nordwick wrote:
    > I'm using MySQLdb and can connect and issue queries that return result sets, but I how do I get the column names for those result sets?
    >
    > >>> c = MySQLdb.connect(*creds)
    > >>> k = c.cursor()
    > >>> k.execute("select * from account")

    > 3L
    > >>> k.fetchall()

    > ((1L, 'test', -1L), (2L, 'Test', -1L), (3L, 'Test2', -1L))
    >
    >
    > -j


    k.description

    See also http://www.python.org/dev/peps/pep-0249/

    HTH,
    ~Simon
     
    Simon Forman, Aug 16, 2006
    #2
    1. Advertising

  3. Jason Nordwick

    Bob Kline Guest

    Jason Nordwick wrote:
    > I'm using MySQLdb and can connect and issue queries that return result
    > sets, but I how do I get the column names for those result sets?


    [d[0] for d in k.description]

    Cheers,
    Bob
     
    Bob Kline, Aug 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    1
    Views:
    335
    Peter Bromberg [C# MVP]
    Feb 21, 2004
  2. Brent
    Replies:
    4
    Views:
    3,452
    Brent
    Sep 15, 2005
  3. Replies:
    2
    Views:
    441
  4. Replies:
    6
    Views:
    1,134
    M.-A. Lemburg
    Jun 27, 2008
  5. Hunt Jon
    Replies:
    2
    Views:
    115
    Phlip
    Dec 18, 2008
Loading...

Share This Page