SQLAlchemy ValueError用于create_engine()的密码斜线 [英] SQLAlchemy ValueError for slash in password for create_engine()

查看:337
本文介绍了SQLAlchemy ValueError用于create_engine()的密码斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个看起来很简单的问题:我的Python脚本正在尝试创建SQLAlchemy数据库连接。密码包含正斜杠:

Fairly simple-looking problem: my Python script is attempting to create a SQLAlchemy database connection. The password contains a forward slash:

engineString = 'postgresql://wberg:pass/word@localhost/mydatabase'
engine = sqlalchemy.create_engine(engineString)

但是第二行出现:

ValueError: invalid literal for int() with base 10: 'pass'

使用原始字符串(以'r'开头)无济于事。有什么办法让SQLAlchemy接受该密码?我通常的下一步是尝试使用下级方法构造连接,但是我看不到在文档中建立连接的另一种方法。我在这里根本不允许使用带斜杠的密码吗?我可以适应这一点,但是如果没有该功能,该工具包似乎不可能走得那么远。

Using a raw string (prepending with 'r') doesn't help. Is there some way to get SQLAlchemy to accept that password? My normal next step would be to try to construct the connection with subordinate methods, but I can't see another way of making a connection in the doc. Am I simply not allowed passwords with slashes here? I can accomodate this, but it seems unlikely that the toolkit could have gotten this far without that feature.

版本:Python 2.6.6,SQLAlchemy 0.8.0

Versions: Python 2.6.6, SQLAlchemy 0.8.0

推荐答案

斜杠不是URL组件字符串的有效字符。您需要对连接字符串的密码部分进行URL编码:

Slashes aren't valid characters for URL component strings. You need to URL-encode the password portion of the connect string:

from urllib import quote_plus as urlquote
from sqlalchemy.engine import create_engine
engineString = 'postgresql://wberg:%s@localhost/mydatabase' % urlquote('pass/word')
engine = create_engine(engineString)

这篇关于SQLAlchemy ValueError用于create_engine()的密码斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆