Python 'float64' cannot be converted to a MySQL type but in manual query thats no problem

て烟熏妆下的殇ゞ 提交于 2021-01-28 02:29:00

问题


I have python script and want insert data to database like this:

(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)

And my table its ok,

In that pict I try to manual insert data to db, and it's work fine. But in my python script return error:

Failed processing format-parameters; Python 'float64' cannot be converted to a MySQL type

I don't know what's wrong with that, because I insert manually and work. I used normal script to insert to db, here my script:

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()

Inside variable detail_encoding :

[(1, -0.12301105260848999), (1, 0.07728659361600876), (1, 0.005048183258622885), (1, -0.03252531960606575), (1, -0.05449267476797104), (1, -0.026811596006155014)]

What's wrong with my code, or how to convert to SQL type of float/decimal from python float64, or any solution for this? Thanks


回答1:


From my knowledge; you should be able to fix this by converting them to python data-types using built in functions, so what you passed would have gone trough something like this:

int(num)

float(decimal)



来源:https://stackoverflow.com/questions/60667627/python-float64-cannot-be-converted-to-a-mysql-type-but-in-manual-query-thats-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!