[Tutor] MySQL api does not accept variable.
Alan Gauld
alan.gauld at freenet.co.uk
Fri Jan 6 13:47:53 CET 2006
def sql_listusers(hostname, dbusername, dbpassword, dbbase, dbasename):
try:
conn = MySQLdb.connect (host = hostname,
user = dbusername,
passwd = dbpassword,
db = dbbase)
cursor = conn.cursor ()
cursor.execute ("""
use %s """, (dbasename)) # <--- here is the problem
> Everything works fine in this function, except that fact that the
> (dbasename) variable is not accepted. I don't know why..
What error are you getting. Are you sure the value in dbasename
is *exactly* the same as the version that worked with a hard
coded value?
> version of the program did not use a variable there. Is there any
> obvious mistake that I can't see?
Given that we can't see what you are passing into the function its
hard to tell.
On a general note, given that your function claims to list users it
would seem reasonable that you could already have a connection
to the database and you would want to pass that in rather than
do all the connect stuff inside the function. Otherwise you will
have to close the database connection before listing the users etc.
That seems pretty inconvenient to me. I'd expect the function
interface to look more like:
def sql_listUsers(dbConnection, dbName): ...
Just a thought,
Alan G.
More information about the Tutor
mailing list