MySQLdb and dictcursor

Christoph Haas email at christoph-haas.de
Mon Jul 24 08:25:58 EDT 2006


On Monday 24 July 2006 14:06, borris wrote:
> doesn anyone know a good reference, tute or examples of
> MySQLdb's dictCursor
> I want to pass dictionaries into the sql exec statements.
> I could only succeed with text as values

A german linux magazin has an article about passing a *list* of items at
http://www.linux-magazin.de/Artikel/ausgabe/2002/06/python-api/python-api.html

For those who don't understand german here comes an example on how to pass 
lists:

cursor.execute("""INSERT INTO Adressen (Name, Strasse, PLZ, Ort)
              VALUES (%s, %s, %s, %s)""",
           [ ('Dr. Hans Mustermann', 'Musterstraße 13', 50823, 'Köln'),
             ('Peter Lustig', 'Im Bauwagen 2', 50827, 'Porz'),
             ('Edmund Stoiber', 'Spendensumpf 1', 47011, 'Bimbesdorf'),
             ('Onkel Hotte', 'Im Siff 42', 57072, 'Siegen'),
             ('Gerhard Schröder', 'Großmaulweg 2', 11901, 'Worthülsen') ]
          )

However the DictCursor is helpful for *returning* dictionaries of values 
instead of just tuples. I always use DictCursor because it makes it 
clearer which rows I want to access in the result. But DictCursor can't 
pass dictionaries in a cursor.execute AFAIKT.

But with dict.iteritems that shouldn't be hard to send dictionary items to 
the SQL database in a loop either.

Regards
 Christoph
-- 
~
~
".signature" [Modified] 1 line --100%--                1,48         All



More information about the Python-list mailing list