如何在传递给text()
的字符串中转义:
以防止SQLAlchemy将其视为bindparameter?
conn.execute(text("select 'My favorite emoticon is :p' from dual")).fetchone()
将导致:
sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) A value is required for bind parameter 'p'
(Background on this error at: http://sqlalche.me/e/14/cd3x)
'
这有点令人困惑,因为从数据库select 'foo :bar baz'
中 Select 字符串的上下文来看,bindparameter在这里没有多大意义.
看起来我可以用\
来逃避这一点,但它说这是不推荐的:
>>> conn.execute(text("select 'My favorite emoticon is \:p' from dual")).fetchone()
<stdin>:1: DeprecationWarning: invalid escape sequence \:
('My favorite emoticon is :p',)