我遇到了非常奇怪的问题 - 我使用 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)
但是当我尝试将一些严肃的内容放入密码时(例如'8yq+aB&k$V
') 连接失败。如何以某种方式“转义”或编码密码,使 sqlalchemy 成功地将其传递给 mysql?
这个问题/答案可能与这个问题/答案重复,所以值得先检查一下当密码包含特殊字符时写入连接字符串 https://stackoverflow.com/questions/1423804/writing-a-connection-string-when-password-contains-special-characters
根据SQLAlchemy 文档 http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html
the string format of the URL is an RFC-1738-style string.
根据RFC-1738 定义 http://www.ietf.org/rfc/rfc1738.txt
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.
为此,我假设您的密码(以及与此相关的用户名)需要进行 URL 编码,以确保正确转义任何此类字符。根据如何在Python中对查询字符串进行urlencode? https://stackoverflow.com/questions/5607551/python-urlencode-string这可以在 python 中使用来完成urlencode()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)