Menu

inserting into tables created on the fly

Help
2005-09-09
2012-09-19
  • bugman_2000

    bugman_2000 - 2005-09-09

    Hello.

    I've been looking all over for an example of what I'm trying to do, but I haven't had any luck finding it. Basically, I want to insert values into a table which has been created on the fly and whose name is in a variable. I've tried a variety of ways to do this. I managed to create the table simply by concatenating the MySQL command string, but because I have some values that need escaping, I want to use the built-in escaping of the %s placeholders.

    However, with the triple quoted string, I don't know how to get the value of the table name into the query. A command along the lines of the one below does not seem to work. Can somebody tell me the right way to do this?

    cursor.execute("""INSERT INTO %s VALUES (%s, %s , %s)""",(tablename, value1, value2, value3))

    Thanks very much,

    Tony

     
    • Andy Dustman

      Andy Dustman - 2005-09-09

      You cannot insert table or column names by the execute() method; only parameter values can be inserted. This is a common feature/limitation of all DB-API interfaces.

      Change:

      cursor.execute("""INSERT INTO %s VALUES (%s, %s , %s)""",(tablename, value1, value2, value3))

      to:

      values = value1, value2, value3
      query = """INSERT INTO %s VALUES (%s)""" % (tablename, ",".join(["%s"]*len(values))
      cursor.execute(query, values)

      or better still:

      def placeholders(values):
      return ",".join(["%s"]*len(values)

      def columnnames(columns):
      return ",".join(["%s" % c for f in columns])

      query = """INSERT INTO %s (%s) VALUES (%s)""" % (tablename, columnnames(columns), placeholders(values))
      cursor.execute(query, values)

      Better still if you use dictionaries; left as an exercise for the reader.

       
    • Lisca75

      Lisca75 - 2005-09-15

      I'm sorry for the stupid question...
      Marco

       
    • Lisca75

      Lisca75 - 2005-09-15

      Hi Andy,
      I've the same problem of Tony.
      I've changed my query format follow your suggestions, but when I run the script I obtain a syntax error.

      this is my code:

      import MySQLdb

      db=MySQLdb.connect(user="pippo", passwd="pluto", db="pippo")
      c=db.cursor()
      value= post.date, post.link, post.title, post.source, post.summary, post.description, post.language
      q="""INSERT INTO %s (%s) VALUES (%s)"""%(sys.argv[1],",".join(["%s"]*len(value))
      c.execute(q, value)

      and the error is:

      File "parser.py", line 97
      c.execute(q, value)
      ^
      SyntaxError: invalid syntax

      can yu help me?

      thank you very much
      Marco

       
      • Andy Dustman

        Andy Dustman - 2005-09-15

        I think you left off a closing parenthesis on the previous line.

         

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.