I\'m trying to insert a python variable into a MySQL table within a python script but it is not working. Here is my code
add_results=(\"INSERT INTO account_
If you are on python 3 and above, just use mysqlclient pip install mysqlclient
. It is recommended by django and all the other drivers are bad.
Assuming you are using mysql.connector
(I think you are), define your own converter class:
class NumpyMySQLConverter(mysql.connector.conversion.MySQLConverter):
""" A mysql.connector Converter that handles Numpy types """
def _float32_to_mysql(self, value):
return float(value)
def _float64_to_mysql(self, value):
return float(value)
def _int32_to_mysql(self, value):
return int(value)
def _int64_to_mysql(self, value):
return int(value)
config = {
'user' : 'user',
'host' : 'localhost',
'password': 'xxx',
'database': 'db1'
}
conn = mysql.connector.connect(**config)
conn.set_converter_class(NumpyMySQLConverter)
One of your passed values could be of type numpy.float64
which is not recognized by the MySQL connector. Cast it to a genuine python float
on populating the dict.