Discussion:
How to (quick) check if a table exists in schema?
Massi
2012-04-25 13:57:28 UTC
Permalink
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?
Thanks in advance!
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Wichert Akkerman
2012-04-25 14:11:20 UTC
Permalink
Post by Massi
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
metadata = MetaData(engine)
table = Table(table_name, metadata, autoload=True)
return table
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 ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Massi
2012-04-25 14:33:17 UTC
Permalink
Thank you for your reply Wichert, I already used the Inspector method
get_table_names(), but using that I'd have to check if a table name is
present in a vector which can have 100.000 elements. This can be even
slower if done for lots of times. Maybe I can perform a binary search,
but I'm not sure that the resulting vector is ordered. Am I wrong?
Other Ideas?
Post by Wichert Akkerman
Post by Massi
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
         metadata = MetaData(engine)
             table = Table(table_name, metadata, autoload=True)
             return table
             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?
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=insp...
Wichert.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Michael Bayer
2012-04-25 15:15:03 UTC
Permalink
dialect has a has_table method:

engine.dialect.has_table(engine.connect(), "mytable")
Post by Massi
Thank you for your reply Wichert, I already used the Inspector method
get_table_names(), but using that I'd have to check if a table name is
present in a vector which can have 100.000 elements. This can be even
slower if done for lots of times. Maybe I can perform a binary search,
but I'm not sure that the resulting vector is ordered. Am I wrong?
Other Ideas?
Post by Wichert Akkerman
Post by Massi
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
metadata = MetaData(engine)
table = Table(table_name, metadata, autoload=True)
return table
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?
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=insp...
Wichert.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
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 ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Loading...