By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,505 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,505 IT Pros & Developers. It's quick & easy.

MySQLdb and dictcursor

P: n/a
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
this is how I got the cursor object to call cursor.execute("query")
connection = MySQLdb.connect( host = "localhost", user =
self.config.userName, passwd = self.config.password, cursorclass =
MySQLdb.cursors.DictCursor)

thanks
Jul 24 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/...ython-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
Jul 24 '06 #2

P: n/a
Dennis Lee Bieber <wl*****@ix.netcom.comwrote in
news:du********************************@4ax.com:
dictCursor
RETURNS the results as a dictionary; it doesn't affect how
parameters are passed in.
thats how I was using it

>
Normally results are a (list or tuple) where you have to know the
order of the fields specified in the query:

cr.execute("select a, c, b from table")
dt = cr.fetchone()

dt is a (list/tuple) with (a_value, c_value, b_value)

With a dictCursor you get

dcr.execute("select a, c, b from table")
ddt = dcr.fetchone()

ddt is a dictionary of {"a" : a_value, "b" : b_value, "c" : c_value}
MySQLdb nominally uses just the %s placeholder style, but I think
it
will also function with %(name)s format...

cr.execute(
"insert into table (c, a, b) values (%(c)s, %(a)s, %(b)s",
ddt)
sounds a lot simpler, ill give it a go later. thanks
Jul 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.