Although the most voted answer (by @danmichaelo) certainly works, I wanted to provide my version that solves a major "but" that is already pointed out by @Tominator; custom converter classes must now inherit MySQLConverterBase
and not MySQLConverter
. The reason you do not want to inherit MySQLConverter
(even if it inherits MySQLConverterBase
as @danmichaelo points out) is that it will invoke the corresponding converter in the MySQLConverter
class on each returned value. This will implement a logic you may not want.
To avoid the above, you have two choices. First, you could create a higher-level function that will get the data and alter the rows after they are retrieved.
def get_data_from_db(cursor, sql_query):
cursor.execute(sql)
row = cursor.fetchone()
while row is not None:
row_to_return = row.decode('utf-8') if isinstance(row, bytearray) else row
row = cursor.fetchone()
return row
If you still want to use the custom converter class method, then you should inherit the MySQLConverterBase
as suggested in the documentation (https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html this is valid until mysql-connector-python==8.0.26, see below) and then you could extend the MySQLConverterBase.to_python
method.
class MyConverter(mysql.connector.conversion.MySQLConverterBase):
def to_python(self, vtype, value):
"""Convert MySQL data type to Python"""
if isinstance(value, bytearray):
return value.decode('utf-8')
super().to_python(vtype, value)
P.S. The class MyConverter can be used to implement custom converters by creating functions with names as in the MySQLConverter
class (find the class here: https://github.com/mysql/mysql-connector-python/blob/master/lib/mysql/connector/conversion.py). For example, I was looking to convert TINYINT to bool and added a method named MyConverter._TINY_to_python(self, value, desc=None)
-- Update mysql-connector-python==8.0.27 --
In the 8.0.27 version, if you create a converter class that inherits the MySQLConverterBase you will probably get an error saying "expected a bytes-like object, str found". It is unclear to me why this happens but my answer above on creating the custom converter seems to not hold anymore. Instead, one should inherit the MySQLConverter
class now:
class MyConverter(mysql.connector.conversion.MySQLConverter):
def to_python(self, vtype, value):
"""Convert MySQL data type to Python"""
if isinstance(value, bytearray):
return value.decode('utf-8')
super().to_python(vtype, value)