Sometimes, we want to write a connection string when password contains special characters with Python SQLalchemy.
In this article, we’ll look at how to write a connection string when password contains special characters with Python SQLalchemy.
How to write a connection string when password contains special characters with Python SQLalchemy?
To write a connection string when password contains special characters with Python SQLalchemy, we should URL encode our string.
For instance, we write
from urllib import quote_plus as urlquote
from sqlalchemy.engine import create_engine
engine = create_engine('postgres://user:%s@host/database' % urlquote('badpass'))
to call create_engine
with a connection string that we create by call urlquote
on the password string to URL encode the password.
And then we interpolate that into the connection string.
Conclusion
To write a connection string when password contains special characters with Python SQLalchemy, we should URL encode our string.