Python多行SQL语句,插入带单引号的变量

时间:2019-04-08 20:24:53

标签: sql python-3.x

问题类似于Pythonic way to create a long multi-line string

但是,我需要在查询中插入一个用单引号引起来的变量。不能使它正常工作。每当我输出查询时,我只会得到2019-03-13,我认为应该是'2019-03-13'

businessDate = '2019-03-13'


    sql = f"""   
#query goes in here.
 and businessdate = {businessDate}

    """

错误消息:

': ('42883', '[42883] ERROR 4286: Operator does not exist: date = int\nHINT: No operator matches the given name and argument type(s). You may need to add explicit type casts\n (4286) (SQLExecDirectW)')

2 个答案:

答案 0 :(得分:1)

只需将其设为原始字符串即可

businessDate = r"'2019-03-13'"

答案 1 :(得分:1)

实现字符串插值的Python方法是

businessDate = '2019-03-13'
sql = """   
 #query goes in here.
 and businessdate = '%s'
""" %businessDate

print(sql) #to check the end result