gpt4 book ai didi

python - 在 python 中读取 MySQL blob

转载 作者:行者123 更新时间:2023-11-29 02:20:54 24 4
gpt4 key购买 nike

您好,在我从查询中获取 blob 后,我遇到了类型问题,这是代码

conn = MySQLdb.connect("mysqlblah", "user", "pass", "db")

cursor = conn.cursor()

data = []
queryString = "SELECT * FROM contentindex where tag LIKE '" + contentType + "%'"
cursor.execute(queryString)

rows = cursor.fetchall()
columns = [t[0] for t in cursor.description]
for row in rows:
jsonRow = {}
i = 0
for column in columns:
if column == "created":
jsonRow[column] = str(row[i])
elif column == "icon":
icon = row[i]
print icon
jsonRow[column] = "data:image/(jpg);base64," + base64.b64encode(icon.getvalue())
else:
jsonRow[column] = row[i]
i = i + 1
data.append(jsonRow)

这会打印出 <_io.BytesIO object at 0x01667810>然后抛出 'str' object has no attribute 'getvalue'异常。

我在这个问题上苦苦思索了好几天,非常感谢任何帮助

最佳答案

看起来 MySQL 转换了 BLOB字段类型到 Python str .

根据我们在评论中的对话,我相信您无意中存储了一个值为 "<_io.BytesIO object at 0x01667810>" 的字符串在此列中,而不是您希望存储的实际数据。您可以通过几种不同的方式测试是否属于这种情况:

print icon[0]  # I'm betting it will be '<'
print type(icon) # Likely 'str'

总而言之,您遇到的似乎是数据损坏问题,而不是某种类型的异常问题。

关于python - 在 python 中读取 MySQL blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com