Reputation: 375
In my company we have a MongoDB collection with some information about power sources. I'm trying to do a python scritp to take that information and put it into a MySQL DB.
The problem is that I'm getting an error while loading the data to MySQL. This is the error:
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
Then, this is the python code:
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)
This is the MySQL table structure:
And this is an example from the MongoDB result:
Where am I failing at? Do I have to make a conversion of the data type? How can I do it? I'm using pymongo and mysql.connector
Thanks!
Upvotes: 5
Views: 8207
Reputation: 375
I did what @DDhaliwal suggested and it worked.
idFuente = int(x['idFuente'])
Thanks!
Upvotes: 7