When executing the following code:
import mysql.connector
connection = mysql.connector.connect(...) # connection params here
cursor = connection.cursor()
cursor.execute('create table test_table(value blob)')
cursor.execute('insert into test_table values (_binary %s)', (np.random.sample(10000).astype('float').tobytes(),))
cursor.execute('select * from test_table')
cursor.fetchall()
I get the following error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf7 in position
1: invalid start byte
(...and then a stack trace which I don't think is useful here)
It seems that mysql connector converts my blob to string (and fails to do so). How can I fetch this data as bytes without any conversion?
解决方案
Apparently, this is a known issue with the Python 'mysql' module. Try to use 'pymysql' instead.