MySQLdb DictCursor scrambles field order
sholden at holdenweb.com
Thu Aug 1 13:34:08 EDT 2002
"HW" <google at thegoldensun.com> wrote in message
news:62e69c0c.0207312258.71bc5c59 at posting.google.com...
> "Chris Gonnerman" <chris.gonnerman at newcenturycomputers.net> wrote in
message news:<mailman.1028093265.3963.python-list at python.org>...
> > ----- Original Message -----
> > From: "HW" <google at thegoldensun.com>
> > > Hello,
> > >
> > > I am viewing some MySQL tables via Python CGI; I use the
> > > MySQLdb.DictCursor in order to return the field names as well as the
> > > data, so I can output a table with the field names as column headings.
> > >
> > > Unfortunately, the DictCursor seems to scramble the order of fields,
> > > eg:
> > > "SELECT field1, field2, field3 FROM table"
> > > which returns the fields in order when using a standard cursor, seems
> > > to rearrange the order (randomly?) with a DictCursor.
> > What else did you expect? The fields are returned in the form of a
> > mapping, which has no required or inate order to it. It's a hash.
> > Again, why do you care?
> I care because I am returning the results of an arbitrary
> (user-specified) query. I have considered parsing the SELECT clause of
> their SQL query, and generating a list of keys from this, as you
> suggest, but some of the SELECT clauses are ...a little complex, as in
> the sample below:
[ ... ]
I'm not sure why you're trying to use a DictCursor when you don't appear to
want to access the retrieved columns by name. A standard cursor will give
you the columns in the order they were specified in the retrieval statement.
Your situation was the kind of thing I had in mund when I wrote
which may or may not give you some useful hints.
More information about the Python-list