zangstrell
zangstrell

Reputation: 59

Test connection created by SQLAlchemy

Is it possible to test a connection created in SQLAlchemy? I am starting my studies in SQLAlchemy and in my project, i allow the user to build the engine corresponding to his database. Assuming that i want to check if a user used the correct parameters in the creation of the engine, would i be able to test it and notify him if any parameter is wrong?

EX:

some_engine = create_engine('postgresql://scott:tiger@localhost/')

Session = sessionmaker(bind=some_engine)

This is a correct way to build the engine. Now, suppose that the user name was misspelled, or even the port or host was misspelled, would it be possible to test this connection and point out the error?

Upvotes: 2

Views: 8243

Answers (1)

Petr Blahos
Petr Blahos

Reputation: 2433

conn = some_engine.connect()
conn.close()

connect will raise en error if unsuccessful.

Upvotes: 5

Related Questions