【问题标题】:SQLalchemy not returning BLOB on querying multiple recordsSQLalchemy 在查询多条记录时不返回 BLOB
【发布时间】: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 表 postpost.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


    【解决方案1】:

    显然,问题在于对同一个帖子进行两次查询和解码。

            if pin_post in feed:
                feed.remove(pin_post)
    

    这解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多