SqlAlchemy连接字符串

Ale*_*.P. 6 python mysql sqlalchemy

我遇到了一个非常奇怪的问题 - 我使用sqlalchemy的解决方案无法连接到数据库.这取决于我使用的密码.例如,以下记录是完美的:

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)
Run Code Online (Sandbox Code Playgroud)

但是当我试图把一些严肃的东西放到密码(比如' 8yq+aB&k$V')连接失败时.如何以某种方式"逃避"或编码密码sqlalchemy成功地将其传递给mysql?

Sim*_*tal 11

这个问题/答案可能与此问题重复,因此值得检查此信息首先在密码包含特殊字符时编写连接字符串

根据SQLAlchemy文档

the string format of the URL is an RFC-1738-style string.
Run Code Online (Sandbox Code Playgroud)

根据RFC-1738的定义

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.
Run Code Online (Sandbox Code Playgroud)

为此,我假设您的密码(以及该用户的用户名)需要进行URL编码,以确保正确转义任何此类字符.根据如何在Python中urlencode一个查询字符串?这可以在python中使用urlencode()