Python "int64"无法转换为 MySQL 类型

标签 python mysql mongodb

在我的公司,我们有一个 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表结构:

enter image description here

这是 MongoDB 结果的示例:

enter image description here

我哪里失败了?我必须进行数据类型转换吗?我该怎么做?我正在使用 pymongo 和 mysql.connector

谢谢!

最佳答案

我按照@DDhaliwal的建议做了,结果成功了。

idFuente = int(x['idFuente'])

谢谢!

关于Python "int64"无法转换为 MySQL 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59429917/

相关文章:

php - mysql - 在 mysql 提示符下获取结果,而不是通过脚本?

mysql - SQL COUNT() 在哪个列上运行有什么区别?

php - 如何订购这个特定的内部联接?

node.js - Mongoose 创建多个索引并个性化 quety 以使用特定索引进行调用

node.js - 使用 mongoose 将数据保存到 MongoDB atlas 时出错

python - 为什么 sklearn 预处理 LabelEncoder inverse_transform 仅适用于一列?

python - 如何在 Django-Rest-Framework 中只深入一个领域?

python - 缺少数据,在 Pandas 中插入行并用 NAN 填充

mongodb - 如何在mongoengine中做 "group by"

python - 如何在部署 Scrapy 项目时在 egg 中包含额外的文件和目录