【发布时间】:2021-08-11 19:45:50
【问题描述】:
if current_blog.pin:
pin_post = post.query.get(current_blog.pin)
pin_post.text = pin_post.text.decode("utf-8")
else:
pin_post = False
feed = post.query.filter_by(author=author_id).order_by(post.timestamp.desc()).all()
# Not decoding feed works fine but the code to decode as follows:
#for p in feed:
# print(p.text)
# p.text = p.text.decode("utf-8")
使用 Flask-SQLalchemy,我有一个 MySQL 表 post 和 post.text 类型为 BLOB 的列。我通过使用 str.encode()in 对其进行编码来存储 UTF-8 字符串。查询单个记录时,它返回一个我需要解码的 blob,否则它返回错误cannot use a string pattern on a bytes-like object。但是当查询多条记录时,它会返回一个 str 以便尝试对其进行解码返回错误'str' object has no attribute 'decode'。究竟是什么导致了这种差异?
【问题讨论】:
标签: python mysql sqlalchemy flask-sqlalchemy