我试图编写一些Python代码,调用SQL查询的方式允许SQL查询保持格式化,但是Python(我使用Spider GUI)到了最后of line并认识到字符串没有结束。
让我们说表database.table中的数据如下所示:
ID Date Amount
1 2016-10-01 $3.00
1 2016-10-05 $4.99
1 2016-10-12 $1.29
2 2016-08-12 $50.13
2 2016-09-11 $27.89
2 2016-10-08 $31.13
3 2016-10-04 $4.18
3 2016-10-10 $43.99
3 2016-10-20 $62.11
如果我编写多行代码并尝试将其传递给Python中的单个变量,我会遇到EOF错误。
Date1 = '2016-10-01'
Date2 = '2016-10-10'
TD_Code_Snipit = str("select
ID,
sum(Amount) as Amount
from database.tablename
group by ID
where Date >="
+Date1+
str(" and Date < ")
+Date2+
str(";")
这允许我的代码中的可读性(我想要放入的真正查询大约是80行,因此将它放在一行代码上会使得在运行中进行调整变得相当困难。
这是解决这个问题的一种固有的错误方法,还是Spider中的设置是否使得它不能正确地包裹线导致错误?
答案 0 :(得分:1)
我认为这就是你所需要的。对于多行,您需要'''
,对于变量,您可以使用{}
占位符。
Date1 = '2016-10-01'
Date2 = '2016-10-10'
TD_Code_Snipit = '''select
ID,
sum(Amount) as Amount
from database.tablename
group by ID
where Date >= {}
and Date < {}
;'''.format(Date1, Date2)