473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

MySQLdb and dictcursor

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
2 5477
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
1
by: shearichard | last post by:
Hi - I have written some python to insert a row into a table using MySQLDB. I have never before written SQL/Python using embedded parameters in the SQL and I'm having some difficulties. Could...
1
by: Grzegorz Smith | last post by:
Hi all. I'm trying get data from text field in MySQl 5.0 with my National characters. Data are stored in utf8 encodings. Here is the script: import MySQLdb, MySQLdb.cursors conn =...
3
by: shearichard | last post by:
Hi - I've got SQL that looks like this ... cursor = self.MySQLDb_conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) sqlQuery = "SELECT * FROM T1 WHERE C1 = %s and C2 = %s" sql =...
0
by: Sells, Fred | last post by:
I had some code originally that printed the sql and params when I called the .execute method. I removed it but it still prints. I rebooted and renamed files and still it prints. I am totally...
0
by: Frank Aune | last post by:
Hi, Im using a MySQLdb connection with a DictCursor, and to me it seems the wrapping to dictionaries only prepend column names when there is an actual conflict in the keywords. I would like...
1
by: beef | last post by:
Hello all, I am using MySQLdb 1.2.2 and have a question about the construction of the dictionary keys of a result set. Here is an example query, from which you may intuit some of the...
0
by: Steve Holden | last post by:
Vaibhav.bhawsar wrote: imported The point here is that MySQLdb is a package, not a module. Some packages have their top-level __init__.py import the package's sub-modules or sub-packages to...
1
by: TT | last post by:
I'm trying to using the following code insert a long string into a MySQL table, the data type is of that column is TEXT. When the length of the content is longer than 65K, it get truncated in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.