import dataset
db = dataset.connect(....)
table = db[...]
When I try to insert some value into Mysql table, this error occurred.
Sample Value
You can solve this problem by just adding a float method for every np.float variable like below:
variable = float(variable)
Another option, that could help it is:
from sqlalchemy import event
import numpy as np
import sqlalchemy
engine = sqlalchemy.create_engine(...)
def add_own_encoders(conn, cursor, query, *args):
cursor.connection.encoders[np.float64] = lambda value, encoders: float(value)
event.listen(engine, "before_cursor_execute", add_own_encoders)
Your library tries to format the provided arguments to a format MySQL will understand. To do so, it checks the type of each argument, to determine how the input should be formatted.
However, since your lib doesn't knows numpy.float64
, it fallbacks to a default encoder, which happens to be one for strings (unicode). Here is the relevent piece of code.
def escape_item(val, charset, mapping=None): if mapping is None: mapping = encoders encoder = mapping.get(type(val)) # Fallback to default when no encoder found if not encoder: try: encoder = mapping[text_type] except KeyError: raise TypeError("no default type converter defined") if encoder in (escape_dict, escape_sequence): val = encoder(val, charset, mapping) else: val = encoder(val, mapping) return val
This encoder, assuming the input is indeed a string, tries to call the translate()
method on this string. But, since this method isn't defined for float64, you get this error.
You should try to convert your float64 to a regular float.
Or, you can create your own encoder, and add it in the encoders
dict used as the default mapping of python types to encoder. If you're going to use a lot this lib with float64, it may be worth doing.
Add the command below to anywhere before making pymysql connection. It adds new encoder of numpy.float64.
pymysql.converters.encoders[np.float64] = pymysql.converters.escape_float
pymysql.converters.conversions = pymysql.converters.encoders.copy()
pymysql.converters.conversions.update(pymysql.converters.decoders)