【问题标题】:Python 'float64' cannot be converted to a MySQL type but in manual query thats no problemPython 'float64' 无法转换为 MySQL 类型,但在手动查询中没问题
【发布时间】:2020-03-13 09:09:33
【问题描述】:

我有 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)

我的桌子还可以,

在该图片中,我尝试手动将数据插入数据库,并且工作正常。但是在我的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 类型的浮点/十进制,或任何解决方案?谢谢

【问题讨论】:

    标签: python mysql arrays variables decimal


    【解决方案1】:

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

    int(num)

    float(decimal)

    【讨论】:

    • float() 函数它的工作就像魅力一样,谢谢,但是我如何将它转换回来?
    • 为什么需要将其转换回来? SQL 自己处理。
    • 因为我需要其他地方的数据,我不知道转换为存储到数据库后十进制是否有效,但我会尝试,再次感谢你:)
    猜你喜欢
    • 2021-09-14
    • 2021-11-14
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多