我有一个Python函数,该函数从用户处获取输入并执行一条SQL语句:
def function_name(team, dept start_date, end_date):
dwh = conn.cursor() ## DB Connection established
if (team is None):
dwh.execute(sql.SQL("""select name,dept from employee where group = (%s) and join_date between (%s) and (%s)"""), (group, start_date, end_date))
elif (dept is None):
dwh.execute(sql.SQL("""select name,team from employee where team = (%s) and join_date between (%s) and (%s)"""), (team, start_date, end_date))
else:
dwh.execute(sql.SQL("""select name,dept,team from employee where team = (%s) and dept =(%s) and join_date between (%s) and (%s)"""), (team, dept start_date, end_date))
当我尝试上面的代码时,它在用户提供所有输入时运行。如果未提供任何字段,则返回空输出。
编辑:
当我共享以下输入时返回输出:
function_name("team1", "dept1", "2018-01-01","2018-01-31")
但是,当我在下面输入内容时,它会返回一个空输出(它应该执行脚本的elif (dept is None):
部分)
function_name("team1", "2018-01-01","2018-01-31")
答案 0 :(得分:0)
如果包含团队和部门的输入行,这将很有帮助。
dept后缺少逗号,应修复用户定义的功能。 此外,function_name输入应如下所示:
def function_name(team, dept, start_date, end_date):
if (team is None):
print ("team is none")
elif (dept is None):
print ("dept is none")
else:
print("else")
function_name(team='team1', dept='dept1', start_date='2018-01-01', end_date='2018-01-01')
打印其他内容
function_name(team='team1', dept=None, start_date='2018-01-01', end_date='2018-01-01')
打印部门为空
function_name(team=None, dept='dept1', start_date='2018-01-01', end_date='2018-01-01')
打印团队为空