[Tutor] MySQL api does not accept variable.
Panagiotis Atmatzidis
p.atmatzidis at gmail.com
Fri Jan 6 14:52:51 CET 2006
Hello,
Thank you for the reply. You can browse the entire script code[1]. The
truth is that I did not think about it. The speed is acceptable for
me, but as you point you out, it does not make much sense.
I can't think of the con's and the pro's of keeping open the mysql
connection when the script starts and close it when it ends.. except
the reply speed and the system resources maybe, but with today's
computers (I run this script at a p4 2.6 Ghz 512 RAM with a lot free
memory) I don't think that there will be any real difference.
But, as a starter I am interested in writing -- so called -- "clean
code" and follow the "right way" to do things. Hence if there is a
standard practise I'd like to follow it.
Thank you for your time,
regards
[1] http://beast.merseine.nu/files/other/vuhandle-0.1.html
On 1/6/06, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> 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.
>
>
--
Panagiotis
More information about the Tutor
mailing list