Python多行SQL语句,插入带单引号的变量
问题类似于 但是,我需要在查询中插入一个用单引号括起来的变量。似乎无法使它工作。当我输出我的查询时,我得到的是Python多行SQL语句,插入带单引号的变量,sql,python-3.x,Sql,Python 3.x,问题类似于 但是,我需要在查询中插入一个用单引号括起来的变量。似乎无法使它工作。当我输出我的查询时,我得到的是2019-03-13,我认为应该是'2019-03-13' businessDate = '2019-03-13' sql = f""" #query goes in here. and businessdate = {businessDate} """ 错误消息: :('42883','[42883]错误4286:运算符不存在:date=int\nHIN
2019-03-13
,我认为应该是'2019-03-13'
businessDate = '2019-03-13'
sql = f"""
#query goes in here.
and businessdate = {businessDate}
"""
错误消息:
:('42883','[42883]错误4286:运算符不存在:date=int\nHINT:没有与给定名称和参数类型匹配的运算符。您可能需要添加显式类型转换\n(4286)(SQLExecDirectW)
只需将其设置为原始字符串:
businessDate = r"'2019-03-13'"
只需将其设置为原始字符串:
businessDate = r"'2019-03-13'"
实现字符串插值的Pythonic方法是
businessDate = '2019-03-13'
sql = """
#query goes in here.
and businessdate = '%s'
""" %businessDate
print(sql) #to check the end result
实现字符串插值的Pythonic方法是
businessDate = '2019-03-13'
sql = """
#query goes in here.
and businessdate = '%s'
""" %businessDate
print(sql) #to check the end result
谢谢anton,如果我想在函数中使用
businessDate
作为参数,该怎么办。例如,defstack(businessDate):
不应该有什么区别,只要你使用r“'blah'”语法:就在shell中玩:>>biz_date=r“'1000'”
>print(biz_date)
'1000'
>def blah(blah):
。。。打印(blah)
..
>blah(商业日期)
'1000'
请注意打印转换为str。。。。因此,根据您的功能,您可能需要显式地将其转换为str(blah)对不起,安东,我在后面遇到了问题,我可以给您留言吗?谢谢安东,如果我想在函数中使用businessDate
作为参数,该怎么办。例如,defstack(businessDate):
不应该有什么区别,只要你使用r“'blah'”语法:就在shell中玩:>>biz_date=r“'1000'”
>print(biz_date)
'1000'
>def blah(blah):
。。。打印(blah)
..
>blah(商业日期)
'1000'
请注意打印转换为str。。。。所以,根据您的功能,您可能必须像str(废话)一样显式地强制转换它。对不起,安东,我跟不上,我可以给您留言吗?