在我的公司,我们有一个 MongoDB 集合,其中包含一些有关电源的信息。我正在尝试编写一个 python scritp 来获取该信息并将其放入 MySQL 数据库中。
问题是我在将数据加载到 MySQL 时遇到错误。这是错误:
Traceback (most recent call last): File "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\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 '_int64_to_mysql'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py", line 417, in _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", line 417, in res = [to_mysql(i) for i in res] File "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\conversion.py", line 182, in to_mysql "MySQL type".format(type_name)) TypeError: Python 'int64' cannot be converted to a MySQL type
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "Fuentes y tickets ME.py", line 114, in mycursor.execute(querysql, queryval) File "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py", line 539, in execute psub = _ParamSubstitutor(self._process_params(params)) File "C:\Users\ngabioud\AppData\Local\Programs\Python\Python37\lib\site-packag es\mysql\connector\cursor.py", line 422, in _process_params "Failed processing format-parameters; %s" % err) mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Py thon 'int64' cannot be converted to a MySQL type
然后,这是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
谢谢!
最佳答案
我按照@DDhaliwal的建议做了,结果成功了。
idFuente = int(x['idFuente'])
谢谢!
关于Python "int64"无法转换为 MySQL 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59429917/