Python: Cursor.execute() 接受 2 到 3 个位置参数,但提供了 4 个。

huangapple go评论71阅读模式
英文:

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(&quot;%Y-%m-%d 00:00:00.000&quot;)
dtTill=dtTill.strftime(&quot;%Y-%m-%d 00:00:00.000&quot;)

cursorPBI.execute(&#39;&#39;&#39;SELECT distinct MT 
                     FROM SUBS
                &#39;&#39;&#39;)
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(&#39;&#39;&#39;select distinct MT,SN,GG
                            FROM op
                            where MT in {}
                                and update_date &gt;= date(cast(? as timestamp)) and update_date &lt; date(cast(? as timestamp))
                    &#39;&#39;&#39;
                        .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(&#39;&#39;&#39;select distinct MT,SN,GG
                            FROM op
                            where MT in {}
                                and update_date &gt;= date(cast(? as timestamp)) and update_date &lt; date(cast(? as timestamp))
                    &#39;&#39;&#39;
                        .format(resultPBI_MT),(dtFrom,dtTill)
                    )

But in this case I get another error message:
PrestoUserError(type=USER_ERROR, name=SYNTAX_ERROR, message=&quot;line 1:1: Incorrect number of parameters: expected 2 but found 0&quot;, 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&quot;select distinct MT,SN,GG
                               FROM op
                               where MT in {resultPBI_MT} and
                               update_date &gt;= &#39;{dtFrom}&#39; and 
                               update_date &lt; &#39;{dtTill}&#39;&quot;)

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

huangapple
  • 本文由 发表于 2023年2月27日 03:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定