Question regarding mysqldb module and variable table names

Glenn Scott ezcoder at mailcity.com
Fri Sep 13 23:20:52 EDT 2002


Hi all,

I'm currently using the MySQLdb module to write data to a mySQL
database.

I've run into a problem while changing my code that I was hoping
someone might have some suggestions on.

Originally I had code that looked like this:

c.execute("""INSERT INTO mytable \
        SET playerkey=%s, link=%s, snippet=%s, time_created=%s, \
        last_seen=%s, relevancy=%s, doc_title=%s """, \
         (playerKey, urlLink, snippetText,
self.getCurrentTime(),self.getCurrentTime(),\
         noOfMentions, re.escape(str(self.doc_title))) )

I liked using the function of this type rather than that using the "%"
string formatter since it automatically fixed problems with odd
characters in my data.  Worked great!

I want to change it so that the "mytable" value in this example is a
variable rather than a constant.  So the code would look like this:

 c.execute("""INSERT INTO %s \
        SET playerkey=%s, link=%s, snippet=%s, time_created=%s, \
        last_seen=%s, relevancy=%s, doc_title=%s """, \
         (infoTable, playerKey, urlLink, snippetText,
self.getCurrentTime(), self.getCurrentTime(),\
         noOfMentions, re.escape(str(self.doc_title))) )

But this doesn't work...for whatever reason, using "%s" in an
assignment SET variable works, but as a table name it doesn't.

I suppose I can use the "%" string formatter at the end of the
triplq-quotes to fix this problem, but I'll have to jump through other
hoops to deal with oddness with my data that is taken care of for me
by using the "," alternative.

Any ideas as to how I might approach this?

TIA



More information about the Python-list mailing list