SQLalchemy 在查询多条记录时不返回 BLOB
SQLalchemy not returning BLOB on querying multiple records
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 table post
和 post.text
类型 BLOB
的列。我通过使用 str.encode()
对其进行编码来存储 UTF-8 字符串。查询单个记录时,它 return 是一个我需要解码的 blob,否则它 return 是错误 cannot use a string pattern on a bytes-like object
。但是当查询多条记录时,它 return 是一个 str,因此尝试解码它 return 错误 'str' object has no attribute 'decode'
。究竟是什么造成了这种差异?
显然,问题出在查询和解码相同的 post 两次。
if pin_post in feed:
feed.remove(pin_post)
这解决了问题。
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 table post
和 post.text
类型 BLOB
的列。我通过使用 str.encode()
对其进行编码来存储 UTF-8 字符串。查询单个记录时,它 return 是一个我需要解码的 blob,否则它 return 是错误 cannot use a string pattern on a bytes-like object
。但是当查询多条记录时,它 return 是一个 str,因此尝试解码它 return 错误 'str' object has no attribute 'decode'
。究竟是什么造成了这种差异?
显然,问题出在查询和解码相同的 post 两次。
if pin_post in feed:
feed.remove(pin_post)
这解决了问题。