使用python中的mysql连接器从mysql数据库正确获取blob [英] Properly getting blobs from mysql database with mysql connector in python
本文介绍了使用python中的mysql连接器从mysql数据库正确获取blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
执行以下代码时:
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:无效的起始字节
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)
似乎mysql连接器将我的Blob转换为字符串(但未这样做).如何在不进行任何转换的情况下以字节为单位获取此数据?
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?
推荐答案
显然,这是Python'mysql'模块的已知问题.尝试改用"pymysql".
Apparently, this is a known issue with the Python 'mysql' module. Try to use 'pymysql' instead.
这篇关于使用python中的mysql连接器从mysql数据库正确获取blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文