On 7/24/06, Christoph Haas <[EMAIL PROTECTED]> wrote:
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.

You can use the same syntax you use for string formatting, even without using DictCursor class:

cursor.execute('SELECT * FROM tablename WHERE id = %(id)s', {'id':43})

Regards
Francesco
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to