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

Wells Oliver wells at submute.net
Thu Jul 2 11:57:05 EDT 2009


Will this order at least be the same for that same query every time the
script is executed?

On Thu, Jul 2, 2009 at 10:48 AM, Tim Chase <python.list at tim.thechases.com>wrote:

> 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 Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s'
>> GROUP BY player_id" % (start, date)
>> cursor.execute(sql)
>>
>> 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.)
>>
>
> My guess is you're experiencing the fact that dicts are unordered by nature
> which allows it to return in any order it likes (usually per the internal
> representation/storage).
>
> -tkc
>
>
>
>


-- 
Wells Oliver
wells at submute.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090702/44e67377/attachment.html>


More information about the Python-list mailing list