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)
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