有没有办法在没有百分号conditionally escaped的情况下获取已编译的SQLAlchemy语句?
以下MySQL示例:
from sqlalchemy import Column, Integer, String
from sqlalchemy.dialects import mysql
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import select
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String(80), unique=True)
statement = select([User.name]).where(User.name.like('bob%')).limit(10)
query = str(statement.compile(dialect=mysql.dialect(), compile_kwargs={'literal_binds': True}))
print(query)
生成带有百分号转义的已编译语句:
SELECT users.name
FROM users
WHERE users.name LIKE 'bob%%'
LIMIT 10
而不是:
SELECT users.name
FROM users
WHERE users.name LIKE 'bob%'
LIMIT 10
我收集这是为了确保使用MySQL pyformat参数样式正确格式化字符串。是否只是简单地获得后面的陈述的正确逻辑:
print(query % {})
答案 0 :(得分:0)
通过<{p>>创建自己喜欢的.startswith() ColumOperator会更容易吗?
statement = select([User.name]).where(User.name.startswith('bob')).limit(10)