Menu

Refering to MySQL tables with variables

Help
2002-07-22
2012-09-19
  • shawn carrrethiers

    I'm trying to dynamicly create and edit tables, and i haven't had much luck getting it to work. The most intuitive way I could think to try this is to do some thing like:

    newtablename='a_table_name'
    conn.execute("""CREATE TABLE %s (x int, y int)""",(name,))

    but this does not work, giving a error like:

    error num: 1064, "You have an error in your SQL syntax near ''a_table_name' (x int,y int)'

    I also tried using MySQL's built in variables but that didn't work either, any suggestions?

     
    • Skip Montanaro

      Skip Montanaro - 2002-07-22

      The quoting mechanism only works for parameter values.  Use To substitute for table or column names use Python's % operator instead:

        conn.execute("""CREATE TABLE %s (x int, y int)"""  % name)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.