[Tutor] MySQL api does not accept variable.

Panagiotis Atmatzidis p.atmatzidis at gmail.com
Fri Jan 6 11:59:41 CET 2006


Hello,

This is a snip of my code:

<code>

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
     cursor.execute("""
                    select * from ftpuser;
                    """)
     rows = cursor.fetchall()
     print ""
     print "USER DATABASE LIST"
     print "------------------"
     for row in rows:
        print "%s, %s" % (row[0], row[1])
     cursor.close ()
     print "------------------"
     print ""
     conn.close ()

  except MySQLdb.Error, e:
       print "Error %d: %s" % (e.args[0], e.args[1])
       sys.exit (1)

</code>

Everything works fine in this function, except that fact that the
(dbasename) variable is not accepted. I don't know why.. this is the
syntax I used from the start in order to pass arguments inside to the
mysql command line, and everything worked fine until now. The first
version of the program did not use a variable there. Is there any
obvious mistake that I can't see?

Happy new year to the list members,

Best Regards

--
Panagiotis


More information about the Tutor mailing list