title: Python using mysql connector list databases LIKE and then use those databases in order and run query
body: <p>I'm trying to write a script using pythong and the mysql-connector library.  The script should connect to the mysql server do a "SHOW DATABASES LIKE 'pdns_%' and then using the results returned by the query use each database and then run another query while using that database. </p>
  
  <p>Here is the code </p>
  
  <pre><code>import datetime
  import mysql.connector
  from mysql.connector import errorcode
  
  
  cnx = mysql.connector.connect (user='user', password='thepassword',
                                 host='mysql.server.com',buffered=True)
  cursor = cnx.cursor()
  
  query = ("show databases like 'pdns_%'")
  
  cursor.execute(query)
  
  databases = query
  
  for (databases) in cursor:
      cursor.execute("USE %s",(databases[0],))
      hitcounts = ("SELECT Monthname(hitdatetime) AS 'Month', Count(hitdatetime) AS 'Hits' WHERE  hitdatetime &gt;= Date_add(Last_day(Date_sub(Curdate(), interval 4 month)), interval 1 day) AND hitdatetime &lt; Date_add(Last_day(Date_sub(Curdate(), interval 1 month)), interval 1 day) GROUP  BY Monthname(hitdatetime) ORDER BY Month(hitdatetime)")
      cursor.execute(hitcounts)
      print(hitcounts)
  
  
  cursor.close()
  cnx.close()
  </code></pre>
  
  <p>When running the script it stops with the following error'd output</p>
  
  <pre><code>Traceback (most recent call last):
    File "./mysql-test.py", line 18, in &lt;module&gt;
      cursor.execute("USE %s",(databases[0],))
    File "/usr/lib/python2.6/site-packages/mysql/connector/cursor.py", line 491, in execute
      self._handle_result(self._connection.cmd_query(stmt))
    File "/usr/lib/python2.6/site-packages/mysql/connector/connection.py", line 635, in cmd_query
      statement))
    File "/usr/lib/python2.6/site-packages/mysql/connector/connection.py", line 553, in _handle_result
      raise errors.get_exception(packet)
  mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''pdns_382'' at line 1
  </code></pre>
  
  <p>Based on the error I'm guessing there is an issue with how its doing the datbase name from the first query.  Any pointers in the correct direction would be very helpful as I'm very much a beginner.  Thank you very much. </p>