MySQL简单的存储图片信息

假装没事ソ 提交于 2019-11-27 23:52:35

MySQL存储图片的二进制,其字段设置为blob属性,二进制数据

1、连接数据库

1 import pymysql
2 import sys
3  
4 conn=pymysql.connect(host='localhost',user='root',passwd='xxx',db='mydata')

2、打开存储图片路径

1 fp = open("./1.jpg")
2 img = fp.read()
3 fp.close()

3、存储图片

 1 def insert_imgs(img):
 2     # mysql连接
 3  
 4     cursor = conn.cursor()
 5     # 注意使用Binary()函数来指定存储的是二进制
 6     # cursor.execute("insert into img set imgs='%s'" % mysql.Binary(img))
 7     cursor.execute("Insert into img(imgs) values(%s)", (mysql.Binary(img)))
 8     # 如果数据库没有设置自动提交,这里要提交一下
 9     conn.commit()
10     cursor.close()
11     # 关闭数据库连接
12     conn.close()

4、提取图片

1  def select_imgs(img):
2     cursor=conn.cursor()
3     cursor.execute('select imgs from img')
4     print cursor.fetchall()
5     cursor.close()
6     conn.close()

版权声明:本文为CSDN博主「Coder_py」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Coder_py/article/details/77368034

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!