On 04/25/2012 03:57 PM, Massi wrote:
Hi everyone,

in my script I have to deal with a huge database with thousands of
tables. Given a table name (a python string) I would have to now if
such a table exists or not. Up to now I have written this function:

def DBGetTableByName(table_name) :
         metadata = MetaData(engine)
         try :
             table = Table(table_name, metadata, autoload=True)
             return table
         except NoSuchTableError :
             return None

I use its return value to check if the table exists, but the problem
is that it is too slow. Since I have to repeat this operation several
times I wonder if there is a faster (and smarter) way to perform this
control.
Any hints?

Use the inspector:

from sqlalchemy.engine.reflection import Inspector

inspector = Inspector.from_engine(engine)
print table_name in inspector.get_table_names()

You can find the documentation here: http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html?highlight=inspector#sqlalchemy.engine.reflection.Inspector

Wichert.

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