【发布时间】:2020-04-13 05:49:45
【问题描述】:
在我的公司,我们有一个 MongoDB 集合,其中包含一些有关电源的信息。我正在尝试做一个 python 脚本来获取该信息并将其放入 MySQL 数据库中。
问题是我在将数据加载到 MySQL 时遇到错误。这是错误:
Traceback(最近一次调用最后一次):文件 "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\conversion.py",第 179 行,在 to_mysql 中 return getattr(self, "_{0}_to_mysql".format(type_name))(value) AttributeError: 'MySQLConverter' 对象没有属性 '_int64_to_mysql'
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件 "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py",第 417 行,在 _process_params res = [to_mysql(i) for i in res] 文件 "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py",第 417 行,在 res = [to_mysql(i) for i in res] 文件 "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\conversion.py",第 182 行,在 to_mysql 中 "MySQL type".format(type_name)) TypeError: Python 'int64' 无法转换为 MySQL 类型
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次通话最后一次):文件“Fuentes y Tickets ME.py”, 第 114 行,在 mycursor.execute(querysql, queryval) 文件“C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py",第 539 行,在执行中 psub = _ParamSubstitutor(self._process_params(params)) 文件“C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py",第 422 行,在 _process_params “处理格式参数失败;%s”% err) mysql.connector.errors.ProgrammingError: 处理失败 格式参数; Py thon 'int64' 无法转换为 MySQL 类型
那么,这是python代码:
idFuente = x['idFuente'] #Tried np.int64(x['idFuente']) but still not working
direccion = x['direccion']['calle'] + " " + str(x['direccion']['nro'])
nodoUbicacion = x['direccion']['nodoUbic']
nodoAlimenta = x['nodoAlimenta']
tieneTransponder = x['tieneTransponder']
tuvoTransponder = x['tuvoTransponder']
macTp = x['macTp']
tieneBaterias = x['tieneBaterias']
tuvoBaterias = x['tuvoBaterias']
try:
tp_psBatteriesPerString = x['info_poleo']['tp_psBatteriesPerString']
tp_psBatteryVoltage_String_1_Battery_1 = x['info_poleo']['tp_psBatteryVoltage_String_1_Battery_1']
tp_psBatteryVoltage_String_1_Battery_2 = x['info_poleo']['tp_psBatteryVoltage_String_1_Battery_2']
tp_psBatteryVoltage_String_1_Battery_3 = x['info_poleo']['tp_psBatteryVoltage_String_1_Battery_3']
tp_psBatteryVoltage_String_2_Battery_1 = x['info_poleo']['tp_psBatteryVoltage_String_2_Battery_1']
tp_psBatteryVoltage_String_2_Battery_2 = x['info_poleo']['tp_psBatteryVoltage_String_2_Battery_2']
tp_psBatteryVoltage_String_2_Battery_3 = x['info_poleo']['tp_psBatteryVoltage_String_2_Battery_3']
tp_psTotalStringVoltage = ['info_poleo']['tp_psBatteryVoltage_String_2_Battery_3']
except:
tp_psBatteriesPerString = None
tp_psBatteryVoltage_String_1_Battery_1 = None
tp_psBatteryVoltage_String_1_Battery_2 = None
tp_psBatteryVoltage_String_1_Battery_3 = None
tp_psBatteryVoltage_String_2_Battery_1 = None
tp_psBatteryVoltage_String_2_Battery_2 = None
tp_psBatteryVoltage_String_2_Battery_3 = None
tp_psTotalStringVoltage = None
querysql = "INSERT INTO fuentes (`idFuente`, `direccion`, `nodoUbicacion`, `nodoAlimenta`, `tieneTransponder`, `tuvoTransponder`, `macTp`, `tieneBaterias`, `tuvoBaterias`, `tp_psBatteriesPerString`, `tp_psBatteryVoltage_String_1_Battery_1`, `tp_psBatteryVoltage_String_1_Battery_2`, `tp_psBatteryVoltage_String_1_Battery_3`, `tp_psBatteryVoltage_String_2_Battery_1`, `tp_psBatteryVoltage_String_2_Battery_2`, `tp_psBatteryVoltage_String_2_Battery_3`, `tp_psTotalStringVoltage`, `fechaCarga`) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
queryval= (idFuente, direccion,nodoUbicacion,nodoAlimenta,tieneTransponder,tuvoTransponder, macTp, tieneBaterias, tuvoBaterias, tp_psBatteriesPerString, tp_psBatteryVoltage_String_1_Battery_1,tp_psBatteryVoltage_String_1_Battery_2,tp_psBatteryVoltage_String_1_Battery_3,tp_psBatteryVoltage_String_2_Battery_1,tp_psBatteryVoltage_String_2_Battery_2,tp_psBatteryVoltage_String_2_Battery_3,tp_psTotalStringVoltage,fechaCarga)
mycursor.execute(querysql, queryval)
这是 MySQL 表结构:
这是来自 MongoDB 结果的示例:
我在哪里失败了?我必须转换数据类型吗?我该怎么做?我正在使用 pymongo 和 mysql.connector
谢谢!
【问题讨论】:
-
你能打印 value queryval 吗?..并在此处添加
-
你可以试试
idFuente = int(x['idFuente']) -
我们需要看看你是如何从 mongodb 中获取数据的;即
x是如何填充的? -
感谢大家的帮助。 @DDhaliwal 建议有效:)