D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Python Question....



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday 13 May 2003 10:09 am, Neil Stone wrote:
> I have a question relating to python...
>
> I am trying to query a MySQL database and get the result in a readable
> format for me to be able to process...
>
> python code below..
>
> ------------
>
> import _mysql
> import sys
> import os
>
> UserName = os.environ['USER']
>
> # Connect to the MySQL database
> db=_mysql.connect(host="localhost",user="<username>",passwd="<password>
>",db="<dbname>")
>
> db.query("""SELECT UserID FROM tblUsers WHERE UserName = `" + RoomName
> + "`""")
> r=db.store_result()
> r.fetch_row()
>
> -------------
>
> The above code returns the UserID field, as it should, but if I tell
> python to "print r" I get "<result object at 811c588>" or similar.
>
> How can I get the UserID (no, not the linux uid) in to a format that i
> can simply print out to the screen and do other work with later on ?
>
> Neil (Man what not knows python...)

This is a bit of a wild guess: 

Your result object may contain a list or dictionary.  Try accessing it by 
row number and field name, try something like 
print r[0]['UserID']
or
print r.nrows()

Tony
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE+wO1M+ZJrkBuz/nkRAgDFAJ9AgjQ25zNzkrbJOtbquj3ju80atgCfU+LA
9UJz8JP/Pm2llmKevxY7MDY=
=Os/M
-----END PGP SIGNATURE-----

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly