MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor

Wells Oliver wells at
Thu Jul 2 17:32:40 CEST 2009

Is there some kind of mysterious logic to how the the columns are ordered
when executing the following:

sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR)
AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN
Teams ON = Teams.team_id WHERE Date BETWEEN '%s' AND '%s'
GROUP BY player_id" % (start, date)

for row in cursor.fetchall():
    print row.keys()

What I get is:

['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER']

Neither alphabetical nor the order in which they were specified in the query
nor... any seeming order I can suss out. Any ideas? Thanks!

(cursor being a MySQLdb.cursors.DictCursor object.)

Wells Oliver
wells at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list