英文:
Python: Cursor.execute() takes from 2 to 3 positional arguments but 4 were given
问题
I'm trying to use Select statement and passing 3 elements from a Presto DB but I get this error message: Cursor.execute() takes from 2 to 3 positional arguments but 4 were given
even though when the same syntax is used, but for a SQL server, there is no issue.
dtFrom = datetime.today() - timedelta(days=8)
dtTill = datetime.today() - timedelta(days=7)
dtFrom = dtFrom.strftime("%Y-%m-%d 00:00:00.000")
dtTill = dtTill.strftime("%Y-%m-%d 00:00:00.000")
cursorPBI.execute('''SELECT distinct MT
FROM SUBS
''')
resultPBI_MT = [item[0] for item in cursorPBI.fetchall()]
resultPBI_MT = (tuple(resultPBI_MT))
cursorPBI.close()
connPBI.close()
connLUDP_Searchlight, cursorLUDP_Searchlight = DB_Connections.get_LUDP_Searchlight_connection()
cursorLUDP_Searchlight.execute('''select distinct MT, SN, GG
FROM op
where MT in {}
and update_date >= date(cast(? as timestamp)) and update_date < date(cast(? as timestamp))
'''
.format(resultPBI_MT), dtFrom, dtTill
)
Cursor.execute() takes from 2 to 3 positional arguments but 4 were given
From what I found on the Internet, it seems that the problem occurs here: .format(resultPBI_MT),dtFrom,dtTill
I also tried to have the strings in a separate list like this:
cursorLUDP_Searchlight.execute('''select distinct MT, SN, GG
FROM op
where MT in {}
and update_date >= date(cast(? as timestamp)) and update_date < date(cast(? as timestamp))
'''
.format(resultPBI_MT), (dtFrom, dtTill)
)
But in this case, I get another error message: PrestoUserError(type=USER_ERROR, name=SYNTAX_ERROR, message="line 1:1: Incorrect number of parameters: expected 2 but found 0", query_id=20230226_193923_16108_ir2bh)
英文:
I'm trying to use Select statement and passing 3 elements from a Presto DB but I get this error message: Cursor.execute() takes from 2 to 3 positional arguments but 4 were given
even though when the same syntax is used, but for a SQL server, there is no issue.
dtFrom = datetime.today() - timedelta(days=8)
dtTill= datetime.today() - timedelta(days=7)
dtFrom=dtFrom.strftime("%Y-%m-%d 00:00:00.000")
dtTill=dtTill.strftime("%Y-%m-%d 00:00:00.000")
cursorPBI.execute('''SELECT distinct MT
FROM SUBS
''')
resultPBI_MT= [item[0] for item in cursorPBI.fetchall()]
resultPBI_MT=(tuple(resultPBI_MT))
cursorPBI.close()
connPBI.close()
connLUDP_Searchlight,cursorLUDP_Searchlight = DB_Connections.get_LUDP_Searchlight_connection()
cursorLUDP_Searchlight.execute('''select distinct MT,SN,GG
FROM op
where MT in {}
and update_date >= date(cast(? as timestamp)) and update_date < date(cast(? as timestamp))
'''
.format(resultPBI_MT),dtFrom,dtTill
)
> Cursor.execute() takes from 2 to 3 positional arguments but 4 were given
From what I found on the Internet, it seems that the problem occurs here:.format(resultPBI_MT),dtFrom,dtTill
I also tried to have the strings in a separate list like this
cursorLUDP_Searchlight.execute('''select distinct MT,SN,GG
FROM op
where MT in {}
and update_date >= date(cast(? as timestamp)) and update_date < date(cast(? as timestamp))
'''
.format(resultPBI_MT),(dtFrom,dtTill)
)
But in this case I get another error message:
PrestoUserError(type=USER_ERROR, name=SYNTAX_ERROR, message="line 1:1: Incorrect number of parameters: expected 2 but found 0", query_id=20230226_193923_16108_ir2bh)
答案1
得分: 1
你的字符串格式似乎有问题。尝试使用 f 字符串(示例如下)或在字符串中使用正确的 '{}' 占位符,然后使用 .format(var1, var2, var3)。
cursorLUDP_Searchlight.execute(f"select distinct MT,SN,GG
FROM op
where MT in {resultPBI_MT} and
update_date >= '{dtFrom}' and
update_date < '{dtTill}'")
我省略了日期解析,因为我不知道你在数据库中使用的日期格式。
Python告诉你这是一个错误,因为日期在格式括号外,而 {} 括号不应该包含日期。
英文:
Your string formatting seems to be wrong.
Try using f strings (example down) or put up correct '{}' placeholders in the string followed by .format(var1, var2, var3)
cursorLUDP_Searchlight.execute(f"select distinct MT,SN,GG
FROM op
where MT in {resultPBI_MT} and
update_date >= '{dtFrom}' and
update_date < '{dtTill}'")
I omitted any date parsing as I don't know the format you use in the DB
Python tells you this is an error, because the dates are outside the format parenthesis and the {} parenthesis are not where dates should be
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论