Python“ int64”无法转换为MySQL类型

时间:2019-12-20 18:35:34

标签: python mysql mongodb

在我的公司中,我们有一个MongoDB集合,其中包含有关电源的一些信息。我正在尝试使用python scritp来获取该信息并将其放入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))(value)AttributeError:'MySQLConverter'对象没有属性   '_int64_to_mysql'

     

在处理上述异常期间,发生了另一个异常:

     

回溯(最近通话最近):文件   “ C:\ Users \ ngabioud \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packag   es \ mysql \ connector \ cursor.py“,第417行,在_process_params中       res = [to i的to_mysql(i)in res]文件“ C:\ Users \ ngabioud \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packag   es \ mysql \ connector \ cursor.py“,第417行,在       res = [to i的to_mysql(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类型

     

在处理上述异常期间,发生了另一个异常:

     

回溯(最近通话最近):文件“ 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表结构:

enter image description here

这是来自MongoDB结果的示例:

enter image description here

我在哪里失败?我是否需要转换数据类型?我该怎么做?我正在使用pymongo和mysql.connector

谢谢!

1 个答案:

答案 0 :(得分:0)

我按照@DDhaliwal的建议进行了工作,并且奏效了。

idFuente = int(x['idFuente'])

谢谢!