方法一:
SQL太长,采用'''...'''的方式进行多行录入,占位符{0}、{1}...代替变量
然后在sql语句末尾,使用format()方法将参数传入占位符{0}、{1}...
注意:
{0}表示第一个占位符,{1}表示第二个占位符,
占位符位置有单引号的不可以省略,'{0}'。
此表达式不可以使用execute(sql_1,(day1,day2))传入参数
sql_1='''select count(value) from "D_COLLECT_MANUALDATA"
where collect_id like 'SP%'
and collect_time >= '{0}' ::TIMESTAMP and collect_time <= '{1}' ::TIMESTAMP;
'''.format(day1,day2)
cursor.execute(sql_1)
方法二:
使用双引号包裹SQL语句,在末尾用format()方法传入参数,
注意:使用\转义双引号",
sql_1="select count(value) from \"D_COLLECT_MANUALDATA\" where collect_id like 'SP%' and collect_time >= '{0}' ::TIMESTAMP and collect_time <=