import sqlalchemy as sa
# …
engine = sa.create_engine(connection_uri)
insp = sa.inspect(engine)
print(insp.has_table("team", schema="dbo")) # True (or False, as the case may be)
def table_exists(engine,name):
ins = inspect(engine)
ret =ins.dialect.has_table(engine.connect(),name)
print('Table "{}" exists: {}'.format(name, ret))
return ret
使用SQLAlchemy 1.4+可以通过使用
inspect
对象调用has_table
,如下所示:有关SQLAlchemy的早期版本,请参见另一个答案here
所以我根据第一次休息时的想法制作了这个函数:
相关问题 更多 >
编程相关推荐