1

I want to ask about engine in SQLAlchemy.

Here's my code:

try:
    engine = create_engine('postgres://postgres:pass@localhost:5432/db')
    engine.connect()
except DatabaseError:
    print('Database Error : ', sys.exc_info()[1])

How can I catch an exception for a specific error like "database does not exist" or "wrong password/user name", etc.

1 Answer 1

1

The Python DBAPI doesn't have specific exceptions for these things, so the best you can do is to parse the string:

try:
    # code
except DatabaseError, e:
    if "database does not exist" in str(e):
         # do something
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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