[Tutor] query result set
Steven D'Aprano
steve at pearwood.info
Tue Nov 9 05:09:47 CET 2010
On Mon, Nov 08, 2010 at 05:58:36PM -0800, Shawn Matlock wrote:
> I am able to successfully query a MySQL database using zxJDBC but the result set seems odd to me. It returns the following:
>
> [(u'envDB', u'systest2'), (u'envDir', u'st2'), (u'envCellName', u'Systest2Cell'), (u'envFrontEnd', u'systest2FrontEnd'), (u'envTag', u'system_test2')]
>
> Why is there a "u" before every entry? Can someone point me to an example that puts the results into a List (or Dictionary) without the "u"?
No, you have misunderstood what you are asking for. That is like asking
for lists without [] or dicts without {}.
In Python 2.x, there are two types of strings: byte strings, which
have delimiters " ", and unicode (text) strings, which have delimiters
u" and ". Notice that the u is not part of the string contents, but is
part of the delimiter, just like the " in byte strings, or [ and ] for
lists.
Coming from a database, the strings are Unicode, which means that they could
contain characters that can't be stored in byte-strings. So you have to use
Unicode, which means the object repr() looks like:
u"text inside the quotation marks"
But if you print them, you get:
text inside the quotation marks
*without* the quotation marks included. The slight complication is that
if you put the Unicode string inside a list, dict or tuple, Python prints
the repr() which means you see the u" ". If you don't like it, write a
helper function that prints the list the way you want, or try the pprint
module.
--
Steven
>
> Thank you,
> Shawn
>
>
>
> 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'"
>
> csdbCursor.execute(envsql)
>
> print csdbCursor.fetchall()
>
> csdbCursor.close()
> csdbConn.close()
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list