gpt4 book ai didi

Python 'float64' 无法转换为 MySQL 类型,但在手动查询中没问题

转载 作者:行者123 更新时间:2023-12-04 13:15:42 27 4
gpt4 key购买 nike

我有 python 脚本,想像这样将数据插入数据库:

(1, -0.12301105260848999)
(1, 0.07728659361600876)
(1, 0.005048183258622885)
(1, -0.03252531960606575)
(1, -0.05449267476797104)
(1, -0.026811596006155014)
(1, 0.014027083292603493)
(1, -0.10952591896057129)
(1, 0.15669232606887817)
(1, -0.04698480665683746)
(1, 0.26182907819747925)
(1, -0.015784427523612976)

我的 table 还好,
db

在那张图片中,我尝试手动将数据插入数据库,并且工作正常。但是在我的python脚本中返回错误:
Failed processing format-parameters; Python 'float64' cannot be converted to a MySQL type

我不知道这有什么问题,因为我手动插入并工作。我使用普通脚本插入数据库,这里是我的脚本:
insert_encode_query = """INSERT INTO encoding_data
(id, face_encode)
VALUES (%s, %s)"""

cursor = connection.cursor()
cursor.executemany(insert_encode_query, detail_encoding)
connection.commit()

内部变量 detail_encoding :
[(1, -0.12301105260848999), (1, 0.07728659361600876), (1, 0.005048183258622885), (1, -0.03252531960606575), (1, -0.05449267476797104), (1, -0.026811596006155014)]

我的代码有什么问题,或者如何从 python float64 转换为 SQL 类型的 float/decimal,或者任何解决方案?谢谢

最佳答案

据我所知;你应该能够通过使用内置函数将它们转换为 python 数据类型来解决这个问题,所以你传递的内容会像这样:
int(num)float(decimal)

关于Python 'float64' 无法转换为 MySQL 类型,但在手动查询中没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60667627/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com