I have code that looks like this engine = create_engine("sqlite://") ...initialize stuff... metadata = sqlalchemy.MetaData(engine) metadata.reflect() class Db: pass db = Db() for table in metadata.sorted_tables: setattr(db, table.name, table) query = select([db.bbc.c.name]).where( db.bbc.c.gdp / db.bbc.c.population > select( [(db.bbc.c.gdp / db.bbc.c.population)] ).where( db.bbc.c.name == 'United Kingdom' ) ).where( db.bbc.c.region == 'Europe' ) results = engine.execute(query).fetchall() print query for line in results: print line
It creates an in memory database, initializes it, reflects to get its tables out, and then performs some queries. One thing that's I've noticed is the fact that I have to specify the engine twice: once at the top, when I create the metadata which will contain all the tables, and once at the bottom, when I actually perform the query. In theory, the tables could already know what metadata and what engine they belong to; is it possible to replace the results = engine.execute(query).fetchall() with something like results = query.execute().fetchall() Or similar? It's not a huge deal, but it just bugs me that I have to specify something twice when the computer should know what I want (in this case, what engine I want to use to query), and I was wondering if there was a way to avoid this. Thanks! -Haoyi -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.