# 功能:将图片导入到MySQL数据库
import pymysql
import uuid
def in_database(location):
# 读取图片文件
fp = open(location, 'rb')
img = fp.read()
fp.close()
# 建立一个MySQL连接
database = pymysql.connect(host="127.0.0.1", user="user", passwd="passwd", db="databaseName")
# 存入图片
# 创建游标
cursor = database.cursor()
# 注意使用Binary()函数来指定存储的是二进制
sql = "INSERT INTO tableName VALUES (%s, %s, %s);"
id = str(uuid.uuid4()).replace('-', '')
args = (id, 'xx', img)
cursor.execute(sql, args)
database.commit()
# 关闭游标
cursor.close()
# 关闭数据库连接
database.close()
#print("添加数据库成功!")
opencv获取的图片保存到数据库
import inMysql #文件名
import cv2
#前面的代码省略
cv2.imwrite(
'D:/' + 'no-guard' + '_' + datetime.now().strftime('%H%M%S') + '.jpg',
frame_lwpCV, None)
time = int(datetime.now().strftime('%H%M%S')) #加了时间后缀做区分
inMysql .in_database('d:/' + 'no-guard' + '_' + str(time) + '.jpg')