首页 > 数据库技术 > 详细

Python实现将图片以二进制格式保存到MySQL数据库中,以及取出:

时间:2019-11-15 22:17:45      阅读:596      评论:0      收藏:0      [点我收藏+]

创建数据库表格式:

CREATE TABLE photo ( photo_no int(6) unsigned NOT NULL auto_increment, image MEDIUMBLOB, PRIMARY KEY (`photo_no`) );

Python实现将图片以二进制格式保存到MySQL数据库中:

import sys
import pymysql
from PIL import Image
import os
 
path = "./"

fp = open("./1.png", rb)
img = fp.read()
fp.close()

database = pymysql.connect(host="localhost", user="root", passwd="", db="hua")
cursor = database.cursor()
sql = "INSERT INTO photo (image) VALUES  (%s);"
args = (img)
cursor.execute(sql, args)
database.commit()
cursor.close()
database.close()

print("============")
print("Done! ")

Python实现从MySQL数据库中将二进制格式的图片保存到本地:

import pymysql as mdb
import sys

conn = mdb.connect(host=localhost,user=root,passwd=‘‘,db=hua)
cursor = conn.cursor()
cursor.execute("SELECT image FROM photo LIMIT 1")
fout = open(quchu1.png,wb)
fout.write(cursor.fetchone()[0])
fout.close()
cursor.close()
conn.close()

Python实现将图片以二进制格式保存到MySQL数据库中,以及取出:

原文:https://www.cnblogs.com/ming-4/p/11869825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!