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

taufardh :

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, db

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

Tylerr :

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=297942&siteId=1