This was exactly what I was looking for, thanks a lot! Could have sworn that page didn't use to be there =)
-- Joakim 2009/6/1 Michael Bayer <[email protected]>: > > > why don't you use the new compiler extension ? There's an example of > INSERT...SELECT right there: > > > http://www.sqlalchemy.org/docs/05/reference/ext/compiler.html#synopsis > > > Joakim Lundborg wrote: >> >> Hi list! >> >> I have tried to create a function to return a statement that copies >> data between tables. I have come up with this: >> >> def copy_table(from_table, to_table, whereclause = None): >> columns_str = ", ".join(from_table.columns.keys()) >> from_select = select([columns_str]).where(whereclause) >> from_select_str = str(from_select) >> return text("""INSERT INTO %(to_table)s (%(columns_str)s) >> \n%(from_select_str)s""" % params ) >> >> (Don't know if this is mysql-only syntax, but it works for me at >> least) If there is a better way of doing this, please enlighten me. >> >> Now I'd like to add support for where-clauses, and I'm a bit stuck. I >> tried something like this: >> >> def copy_table(from_table, to_table, whereclause = None): >> columns_str = ", ".join(from_table.columns.keys()) >> from_select = select([columns_str]).where(whereclause) >> from_select_str = str(from_select) >> >> where_str = whereclause.compile() >> params = locals() >> params.update(whereclause.compile().params) >> return text("""INSERT INTO %(to_table)s (%(columns_str)s) \nWHERE >> %(where_str)s""" % params ) >> >> ...which didn't quite work, I end up with %s in my query. >> >> Nicest would be if I could somehow inherit from Insert or >> ClauseElement, so generative where-clauses work, but I can't quite >> figure out how to combine that with the visitor pattern used for the >> different backends. Any hints on how I would accomplish this? >> >> -- Joakim >> >> > >> > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
