Remap Mysql tuple to dictionary

S

Steve Holden

Lawrence said:
Pom wrote: said:
I want to convert a Mysql resulset to a dictionary.


Here's a function that does this one row at a time:

def GetEachRecord(TableName, Fields, Condition, Values, Extra = "") :
"""generator which does an SQL query which can return 0 or more
result rows, yielding each record in turn as a mapping from
field name to field value. TableName can be a single table name,
or a comma-separated list of names for a join. Extra allows
specification of order/group clauses."""
Cursor = sql.conn.cursor() # modify this as appropriate
Cursor.execute \
(
", ".join(Fields)
+
" from "
+
TableName
+
" where "
+
Condition
+
" "
+
Extra,
Values
)
while True :
NextRow = Cursor.fetchone()
if NextRow == None :
Cursor.close()
raise StopIteration
#end if
yield dict(zip(Fields, NextRow))
#end while
#end GetEachRecord

You'd use this something like

for Link in GetEachRecord(...) :
... Link[fieldname] ... blah-blah ...
This is a spectacularly bad (non-)solution to the original problem. It
also shows a fine disregard for readability requirements. I suppose you
are using a generator to avoid data duplication, but for 100,000 records
this could be regarded as a premature optimisation on modern computers.

regards
Steve
 
F

Frank Millman

Steve said:
Can you please stop this incessant carping? c.l.py used to be a fun
place to hang out.

Hey, Steve, don't let it get to you. It's still 98% fun.

I am reminded of a spoof Latin motto from the days of my youth -

NIL ILLEGITIMO CARBORUNDUM

Translation available on request.

Frank Millman
 
F

Fredrik Lundh

Frank said:
I am reminded of a spoof Latin motto from the days of my youth -

NIL ILLEGITIMO CARBORUNDUM

isn't that usually written

Illegitimi non carborundum

?

or is that just due to differences between british latin and american latin ?

</F>
 
F

Frank Millman

Fredrik said:
isn't that usually written

Illegitimi non carborundum

?

or is that just due to differences between british latin and american latin ?

</F>

or my bad memory - my youth was a long time ago :)

Frank
 
T

Tim Chase

NIL ILLEGITIMO CARBORUNDUM
isn't that usually written

Illegitimi non carborundum

or is that just due to differences between british latin and
american latin ?

Wouldn't those differences make it
"carbourundum" vs.
"carborundum" respectively?

:*)

(however, yes, dredging my grade-school Latin, I believe Fredrik
is correct in the common ordering of the phrase, though I can't
vouch for the declension of "illegitimi" vs. "illegitimo")

-tkc
 
S

Simon Brunning

isn't that usually written

Illegitimi non carborundum

According to the Wikipedia, neither is actually correct latin -
or is that just due to differences between british latin and american latin ?

American Latin? Is that Lingua::Romana::perligata?
 
L

Lawrence D'Oliveiro

I suppose you are using a generator to avoid data duplication, but for
100,000 records this could be regarded as a premature optimisation on
modern computers.

I was using a generator to avoid loading all those 100,000 records into
memory at once.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top