0

How can I check if a database is empty in SQLAlchemy?

I know you can do inspector.dialect.has_table(engine.connect(), 'table_name') to check for the existence of a single table, but I want to know if there are any tables at all in the database.

1 Answer 1

1

You can check tables in current database:

db.engine.table_names()

or after creating engine variable from connection string:

engine = sql.create_engine("connection_string")

sql.inspect(engine).get_table_names()
Sign up to request clarification or add additional context in comments.

2 Comments

For the scenario I have mentioned, as you have said, if db.engine.table_names() seems to work fine. Thanks!
@MinuraPunchihewa Good. mark the answer as accepted if your problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.