1.数据表如下
2.python代码
import base64
import pymysql
with open('./timg.jpg', 'rb') as f:
data = base64.b64encode(f.read())
conn = pymysql.connect(host="localhost", user="admin", passwd="password", database="test", charset="utf8")
cur = conn.cursor()
cur.execute("insert into image values (1, 'a.jpg', '{}');".format(str(data)[2:-1]))
conn.commit()
cur.close()
conn.close()
conn = pymysql.connect(host="localhost", user="admin", passwd="password", database="test", charset="utf8")
cur = conn.cursor()
cur.execute("select content from image;")
data = cur.fetchone()[0].decode('utf-8')
cur.close()
conn.close()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Base64图片解析</title>
</head>
<body>
<img src="data:image/jpg;base64,此处是数据库查询到的数据"
/>
</body>
</html>
预览