[Tutor] put query result set into dictionary

Joel Goldstick joel.goldstick at gmail.com
Thu Nov 11 01:19:24 CET 2010


On Wed, Nov 10, 2010 at 5:18 PM, Shawn Matlock <matlocs at odscompanies.com>wrote:

>  Let’s see if I can ask the right questions the right way. I am trying to
> put the results of a MySQL query into a dictionary. It fails because it
> includes the Unicode delimiter - “u’somevalue’”. Can someone tell me the
> right way to do this, please?
>
>
>
> Thank you,
>
> Shawn
>
> Notice that you are returning strings.  "u'somevalue'" . Your problem is
not with the unicode, its with the quotes that wrap the unicode delimiter.
I haven't used zxJDBC object, but I am guessing if you look into its
documentation you can return what you really want, which is a unicode
string, not a string containing the representation of a unicode string

>
>
> csdbConn = zxJDBC.connect("jdbc:mysql://myhost:3306/mydb", "User",
> "Password", "com.mysql.jdbc.Driver")
>
>
>
> csdbCursor = csdbConn.cursor(1)
>
>
>
> envsql = "select envVariable, envValue from ODS_ENV_DICT where envName =
> 'ST2'"
>
> print 'envsql: ' + envsql
>
>
>
> csdbCursor.execute(envsql)
>
>
>
> envDict = {}
>
> for envVariable, envValue in csdbCursor.fetchall():
>
>     print envVariable, envValue
>
>     envDict[envVariable].append(envValue)
>
>
>
> csdbCursor.close()
>
> csdbConn.close()
>
>
>
> ------------------
>
> envsql: select envVariable, envValue from ODS_ENV_DICT where envName =
> 'ST2'
>
> envDB systest2
>
> Traceback (most recent call last):
>
>   *File "H:\workspace\test\src\root\nested\example.py", line 33, in
> <module>*
>
>     envDict[envVariable].append(envValue)
>
> KeyError: u'envDB'
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101110/7f4a77ff/attachment.html>


More information about the Tutor mailing list