【发布时间】: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,)(作为导致错误的一个示例)。
-
你检查我的答案了吗?如果正确请不要忘记采纳。谢谢!