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

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

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.

  1. dtFrom = datetime.today() - timedelta(days=8)
  2. dtTill = datetime.today() - timedelta(days=7)
  3. dtFrom = dtFrom.strftime("%Y-%m-%d 00:00:00.000")
  4. dtTill = dtTill.strftime("%Y-%m-%d 00:00:00.000")
  5. cursorPBI.execute('''SELECT distinct MT
  6. FROM SUBS
  7. ''')
  8. resultPBI_MT = [item[0] for item in cursorPBI.fetchall()]
  9. resultPBI_MT = (tuple(resultPBI_MT))
  10. cursorPBI.close()
  11. connPBI.close()
  12. connLUDP_Searchlight, cursorLUDP_Searchlight = DB_Connections.get_LUDP_Searchlight_connection()
  13. cursorLUDP_Searchlight.execute('''select distinct MT, SN, GG
  14. FROM op
  15. where MT in {}
  16. and update_date >= date(cast(? as timestamp)) and update_date < date(cast(? as timestamp))
  17. '''
  18. .format(resultPBI_MT), dtFrom, dtTill
  19. )

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:

  1. cursorLUDP_Searchlight.execute('''select distinct MT, SN, GG
  2. FROM op
  3. where MT in {}
  4. and update_date >= date(cast(? as timestamp)) and update_date < date(cast(? as timestamp))
  5. '''
  6. .format(resultPBI_MT), (dtFrom, dtTill)
  7. )

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.

  1. dtFrom = datetime.today() - timedelta(days=8)
  2. dtTill= datetime.today() - timedelta(days=7)
  3. dtFrom=dtFrom.strftime(&quot;%Y-%m-%d 00:00:00.000&quot;)
  4. dtTill=dtTill.strftime(&quot;%Y-%m-%d 00:00:00.000&quot;)
  5. cursorPBI.execute(&#39;&#39;&#39;SELECT distinct MT
  6. FROM SUBS
  7. &#39;&#39;&#39;)
  8. resultPBI_MT= [item[0] for item in cursorPBI.fetchall()]
  9. resultPBI_MT=(tuple(resultPBI_MT))
  10. cursorPBI.close()
  11. connPBI.close()
  12. connLUDP_Searchlight,cursorLUDP_Searchlight = DB_Connections.get_LUDP_Searchlight_connection()
  13. cursorLUDP_Searchlight.execute(&#39;&#39;&#39;select distinct MT,SN,GG
  14. FROM op
  15. where MT in {}
  16. and update_date &gt;= date(cast(? as timestamp)) and update_date &lt; date(cast(? as timestamp))
  17. &#39;&#39;&#39;
  18. .format(resultPBI_MT),dtFrom,dtTill
  19. )

> 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

  1. cursorLUDP_Searchlight.execute(&#39;&#39;&#39;select distinct MT,SN,GG
  2. FROM op
  3. where MT in {}
  4. and update_date &gt;= date(cast(? as timestamp)) and update_date &lt; date(cast(? as timestamp))
  5. &#39;&#39;&#39;
  6. .format(resultPBI_MT),(dtFrom,dtTill)
  7. )

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

  1. cursorLUDP_Searchlight.execute(f"select distinct MT,SN,GG
  2. FROM op
  3. where MT in {resultPBI_MT} and
  4. update_date >= '{dtFrom}' and
  5. 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)

  1. cursorLUDP_Searchlight.execute(f&quot;select distinct MT,SN,GG
  2. FROM op
  3. where MT in {resultPBI_MT} and
  4. update_date &gt;= &#39;{dtFrom}&#39; and
  5. 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:

确定