当执行以下代码时:
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()
我收到以下错误:
UnicodeDecodeError:“utf-8”编解码器无法解码位置中的字节 0xf7
1:无效起始字节
(...然后是堆栈跟踪,我认为这里没有用)
mysql 连接器似乎将我的 blob 转换为字符串(但未能这样做)。如何在不进行任何转换的情况下以字节形式获取这些数据?
我们遇到了同样的问题,即在 MySQL 8.0.13、mysql-connector-python 8.0.13 和 sqlalchemy 1.2.14 中,BLOB 被错误地读回为 UTF-8 字符串。
对我们来说,秘诀是什么?use_pure
MySQL 连接器选项 https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html。默认为use_pure
在 8.0.11 中已更改,新的默认设置是使用 C 扩展。因此,我们设置了该选项:
create_engine(uri, connect_args={'use_pure': True}, ...)
我们的错误和堆栈跟踪的详细信息:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 1: invalid start byte
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
....
File "/usr/local/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 272, in execute
self._handle_result(result)
File "/usr/local/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 163, in _handle_result
self._handle_resultset()
File "/usr/local/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 651, in _handle_resultset
self._rows = self._cnx.get_rows()[0]
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 273, in get_rows
row = self._cmysql.fetch_row()
SystemError: <built-in method fetch_row of _mysql_connector.MySQL object at 0x5627dcfdf9f0> returned a result with an error set
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)