I am using MySql and Python 3.6/3.8 with mysqlclient to maintain a database with a binary field. The code was constructed before my time in Python 2.7 to insert directly into field with binary data. For example, the table looks like
+-------------------------+--------------------------------------------------------------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+--------------------------------------------------------------------+------+-----+------------+-------+
| license_id | varchar(36) | NO | PRI | NULL | |
| data | binary(32) | NO | UNI | NULL | |
+-------------------------+--------------------------------------------------------------------+------+-----+------------+-------+
and the insert command looked like:
dbconn.execute("""INSERT into datatable (
license_id,
data)
VALUES ("{:s}", "{:s}")
""".format(
str(self._license_id),
escape_string(self._data)))
and this would result in Python 2.7 inserting the binary data directly in the command like
INSERT into datatable (license_id, data) VALUES ("XXX-XXX-52", "������C�...\r�x�b24�B")
but in Python 3 the data is a byte array type and cannot be inserted into a string and attempting to decode it results in:
escape_string(self._auth_hash).decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 5: invalid continuation byte
Other questions seem to indicate a viable path to fix this is to convert the binary to hex (here, for example) but is there any way to insert into a Python 3 unicode string without errors as the original code did?
It appears by removing the quotes around "{:s}" and using binascii.b2a_hex(self._data) would have the same intent, but it's not clear this will have the exact same behavior or if there will be problem. I expect this to be a recurring problem and am looking for the solution with the least side effects possible.