Question regarding mysqldb module and variable table names

Tim Roberts timr at probo.com
Sat Sep 14 07:58:43 CEST 2002


ezcoder at mailcity.com (Glenn Scott) wrote:
>
>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.

Not at all.  This should work:

 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))) )

Do you realize you do not need to escape the ends of lines when you use
triple quotes, nor outside the quotes when you are within parentheses?

 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))) )

If ''' are triple quotes, aren't """ sextuple quotes?
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list