【问题标题】:mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL typemysql.connector.errors.ProgrammingError:处理格式参数失败; Python 'tuple' 无法转换为 MySQL 类型
【发布时间】:2019-03-26 17:33:30
【问题描述】:

我是 Python 新手,只是找不到足够的这种模式的示例。运行 Python 3.4,安装了 MySQL Python 连接器。 MySQL 5.7 版。

使用这组代码(我已将用户、密码等的值替换为空格):

import mysql.connector
cnx = mysql.connector.connect(user='', 
                        password='   ',
                        host='    ',
                        database='     ')
cursor = cnx.cursor()  
PortExistsQ = "SELECT Count(*) FROM portfolio WHERE idPortfolio=%s AND YearWeek(Signal_Date)=%s AND Rank=1"
cursor.execute(PortExistsQ,(portID,Yearweek))

我收到此错误。

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\conversion.py", line 179, in to_mysql
    return getattr(self, "_{0}_to_mysql".format(type_name))(value)
AttributeError: 'MySQLConverter' object has no attribute '_tuple_to_mysql'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 417, in _process_params
    res = [to_mysql(i) for i in res]
  File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 417, in <listcomp>
    res = [to_mysql(i) for i in res]
  File "C:\Python34\lib\site-packages\mysql\connector\conversion.py", line 182, in to_mysql
    "MySQL type".format(type_name))
TypeError: Python 'tuple' cannot be converted to a MySQL type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\threading.py", line 920, in _bootstrap_inner
    self.run()
  File "C:\Python34\lib\threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "C:\workspace\DIY Investing\CSI\CalcP.py", line 56, in calcport
    cursor.execute(PortExistsQ,(portID,Yearweek))
  File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 539, in execute
    psub = _ParamSubstitutor(self._process_params(params))
  File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 422, in _process_params
    "Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

SQL 在 MySQL Workbench 中按预期运行。如果 Portfolio 的值为 2,YearWeek 的值为 201644(导致错误的第一个值),MySQL 中的结果是 Count(*) of 1。

一个代码sn-p的

print ("Portfolio", portID, "YearWeek", Yearweek)

产生以下输出

Portfolio 2 YearWeek (201644,)

有什么建议吗?

【问题讨论】:

  • portID 和 Yearweek 的值是多少?
  • 一个例子使用下面的代码sn-p。 print ("Portfolio", portID, "YearWeek", Yearweek),生成以下打印行。投资组合 2 YearWeek (201644,)(作为导致错误的一个示例)。
  • 你检查我的答案了吗?如果正确请不要忘记采纳。谢谢!

标签: python mysql


【解决方案1】:

错误说元组无法转换为 mysql 类型,当您执行 print 语句时,它变得相当明显

print ("Portfolio", portID, "YearWeek", Yearweek)
Produces the following output

Portfolio 2 YearWeek (201644,)

Yearweek 是一个元组,使用 Yearweek[0] 代替

【讨论】:

    【解决方案2】:

    根据您的示例 - Yearweek 是元组 (201644,),您不能将元组用作准备好的语句的值。

    您可以改用这个:

    cursor.execute(PortExistsQ,(portID,Yearweek[0]))
    

    或者

    Yearweek = Yearweek[0]
    cursor.execute(PortExistsQ,(portID,Yearweek))
    

    这样传递给execute 函数的值是201644(它是元组中的第一个值),而不是元组本身。

    【讨论】:

      猜你喜欢
      • 2017-07-01
      • 2021-10-21
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 2021-10-04
      • 2023-03-29
      • 2017-08-22
      • 2023-02-14
      相关资源
      最近更新 更多