getting database column names from query

skip at pobox.com skip at pobox.com
Wed Aug 16 15:48:59 EDT 2006


    Jason> I'm using MySQLdb and can connect and issue queries that return
    Jason> result sets, but I how do I get the column names for those result
    Jason> 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))

For MySQLdb you can initialize the connection so that result rows are
returned as dictionaries.  (See the MySQLdb.cursorclass - I think -
module).  There is also a description attribute of the cursor object.  See
the DB API for details:

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

Skip



More information about the Python-list mailing list