在我的公司,我们有一个MongoDB收集与一些有关电源的信息。我正在尝试使用python来获取这些信息并将其放入MySQL DB中。
问题是,在将数据加载到MySQL时,我得到了一个错误。这是一个错误:
文件"C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\conversion.py",第179行,在to_mysql中返回getattr(self,“_{0}_to_mysql”.format(Type_name))(值) AttributeError:'MySQLConverter‘对象没有属性'_int64_to_mysql’
在处理上述异常的过程中,发生了另一个异常:
追溯(最近一次调用):File "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 File "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 File to_mysql es\mysql\connector\conversion.py“第182行中,to_mysql "MySQL type".format(type_name)) TypeError: Python 'int64‘不能转换为MySQL类型
在处理上述异常的过程中,发生了另一个异常:
回溯(最近一次调用):文件"Fuentes y Fuentes ME.py",第114行,在mycursor.execute(querysql,queryval) File "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py中,第539行,在execute psub = _ParamSubstitutor(self._process_params(params)) File "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 '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
谢谢!
发布于 2019-12-23 19:34:58
我做了@DDhaliwal建议的事情,结果成功了。
idFuente = int(x['idFuente'])谢谢!
https://stackoverflow.com/questions/59429917
复制相似问题