Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlAlchemy connection string [duplicate]

I faced very strange issue - my solution using sqlalchemy cannot connects to database. It depends on password I am using. for example, following records are working perfect:

PWD='123123123'
USR='test_user';
SQLALCHEMY_DATABASE_URI = 'mysql://{}:{}@localhost:3306/test_db'.format(USR, PWD)

#final result is 'mysql://test_user:123123123@localhost:3306/test_db'.format(USR, PWD)

But when I trying to put something serious to password (like '8yq+aB&k$V') connection failed. How to 'escape' or encode password somehow that sqlalchemy passed it to mysql succesfully?

like image 767
Alex G.P. Avatar asked Jul 22 '13 12:07

Alex G.P.


1 Answers

This question/answer is perhaps a duplicate of this one so worth checking this first Writing a connection string when password contains special characters

According to the SQLAlchemy documentation

the string format of the URL is an RFC-1738-style string.

According to the RFC-1738 definition

If the character corresponding to an octet is
reserved in a scheme, the octet must be encoded.  The characters ";",
"/", "?", ":", "@", "=" and "&" are the characters which may be
 reserved for special meaning within a scheme.

To this end, I would assume that your password (and username for that matter) needs to be URL encoded to ensure any such characters are properly escaped. According to How to urlencode a querystring in Python? this can be done in python using urlencode()

like image 147
Simon at My School Portal Avatar answered Oct 13 '22 00:10

Simon at My School Portal