【pycharm读取图片】#把图片整体存入数据库
#encoding =utf-8
import pymysql
conn = pymysql.connect(host= ‘127.0.0.1’,port = 3306,user = ‘root’,passwd=‘root’,
db = ‘test’)
sql = “insert into images(img) values(%s)”
cur = conn.cursor()
#打开文件,二进制读取
f = open(“2.jpg”,mode = “rb”)
imgValues =f.read()
f.close()
try:
cur.execute(sql, imgValues)
conn.commit()
except:
conn.rollback()
finally:
cur.close()
conn.close()