<br><br><div><span class="gmail_quote">On 7/24/06, <b class="gmail_sendername">Christoph Haas</b> <<a href="mailto:email@christoph-haas.de">email@christoph-haas.de</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Monday 24 July 2006 14:06, borris wrote:<br>> doesn anyone know a good reference, tute or examples of<br>> MySQLdb's dictCursor<br>> I want to pass dictionaries into the sql exec statements.<br>> I could only succeed with text as values
<br><br>A german linux magazin has an article about passing a *list* of items at<br><a href="http://www.linux-magazin.de/Artikel/ausgabe/2002/06/python-api/python-api.html">http://www.linux-magazin.de/Artikel/ausgabe/2002/06/python-api/python-api.html
</a><br><br>For those who don't understand german here comes an example on how to pass<br>lists:<br><br>cursor.execute("""INSERT INTO Adressen (Name, Strasse, PLZ, Ort)<br>              VALUES (%s, %s, %s, %s)""",
<br>           [ ('Dr. Hans Mustermann', 'Musterstraße 13', 50823, 'Köln'),<br>             ('Peter Lustig', 'Im Bauwagen 2', 50827, 'Porz'),<br>             ('Edmund Stoiber', 'Spendensumpf 1', 47011, 'Bimbesdorf'),<br>             ('Onkel Hotte', 'Im Siff 42', 57072, 'Siegen'),
<br>             ('Gerhard Schröder', 'Großmaulweg 2', 11901, 'Worthülsen') ]<br>          )<br><br>However the DictCursor is helpful for *returning* dictionaries of values<br>instead of just tuples. I always use DictCursor because it makes it
<br>clearer which rows I want to access in the result. But DictCursor can't<br>pass dictionaries in a cursor.execute AFAIKT.<br><br>But with dict.iteritems that shouldn't be hard to send dictionary items to<br>the SQL database in a loop either.
</blockquote><div><br>You can use the same syntax you use for string formatting, even without using DictCursor class:<br><br>cursor.execute('SELECT * FROM tablename WHERE id = %(id)s', {'id':43})<br></div><br>Regards<br>Francesco
<br></div>