问题
I begin a program by generating a URL object and passing it to create_engine. In a section of code far, far away I would like to find out what this engine is connected to, i.e. the connection URL.
Is there an easy way to do this? Using inspect I can only see how to get the driver type. I can understand if the password component of a connection string was no longer available, but I'm hoping everything else is still available.
Any ideas?
回答1:
This works quite well for me:
log.info("* Using DB: %s" % (engine.url))
回答2:
The Engine class has an attribute url. Although not documented, it is not 'underscore hidden' so I would assume safe to read.
来源:https://stackoverflow.com/questions/38631210/how-can-i-get-a-connection-url-from-an-sqlalchemy-engine-instance