我编写了一个 SQL 查询,它使用从 WTForms 传入的数据作为参数。 如何确保变量上有双引号?
q = """
select * from table
where dt_date >= %(date)s""" % {'date':date}
现在显示为
select * from table
where dt_date >= 23-06-2016
然后抛出错误。如何让它变成:
select * from table
where dt_date >= "23-06-2016"
请您参考如下方法:
Python 不会阻止您在多行字符串中使用双引号。只有当您将它们并排放置时才会出现问题 ("""")。您可以将双引号转义为 \",也可以在双引号和三引号之间留一个空格 ("""")。
转义:
q = """
select * from table
where dt_date >= \"%(date)s\""""%{'date':date}
>>> print q
select * from table
where dt_date >= "asdf"
三引号前的空格:
q = """
select * from table
where dt_date >= "%(date)s" """%{'date':date}
>>> print q
select * from table
where dt_date >= "asdf"
