On Jun 3, 2010, at 11:24 AM, Harry Percival wrote:

> Hi All,
> 
> I'm building a tool to extract info from databases.  The
> user/programmer doesn't have any advance knowledge of the structure of
> the database before they load it, and i want to dynamically generate
> mapped classes for the database.
> 
> i just want to check there isn't some helpful sqlalchemy stuff that
> can make my life easier, cos it feels harder than it should be.
> 
> sqlsoup seems to expect you to know table names ahead of time. i can't
> find a way of extracting a list of table names from  db =
> SqlSoup(engine)  and i'm finding myself generating classes on the fly
> using the type() function.


the engine has a table_names function, also metadata has a reflect() method 
that pulls in a whole schema and you can peek into the metadata.tables 
dictionary.  There is also an Inspector interface that gives you all sorts of 
info about a database.

http://www.sqlalchemy.org/docs/reference/sqlalchemy/connections.html?highlight=table_names#sqlalchemy.engine.base.Engine.table_names
http://www.sqlalchemy.org/docs/reference/sqlalchemy/schema.html?highlight=reflect#sqlalchemy.schema.MetaData.reflect

the inspector appears like it hasn't made it into the docs yet (that's a bug).  
 It's here:

http://www.sqlalchemy.org/trac/browser/lib/sqlalchemy/engine/reflection.py



> 
> stuff like:
> 
> 
> 
> meta.reflect(bind=engine)
> tables = meta.raw_tables
> 
> class MyTable(object):
>    pass
> 
> for t in tables:
>    tempclass = type('Table%d'%counter,(MyTable,),{'engine':self.engine})
>    mapper(tempclass,t)
> 
> then i use a bunch of classfunctions hanging off MyTable to do things
> like return select alls ... anyways, this feels harder than it should
> be.  am i missing something?  or is sqlalchemy simply not really used
> much to work with existing / arbitrary databases?
> 
> -- 
> 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.
> 

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

Reply via email to