问题描述
我一直很难在MysqL表中插入图像。我已经使用QFileDialog来浏览和显示图像。但是,保存后,每当我单击以检查表列中的BLOB时,都不会保存任何内容。我尝试进行一些研究,但我意识到将图像作为文本文件插入sql命令中是错误的。老师,请问,重组我以下代码并实现插入图像的最佳方法是什么?
class TestReg(QWidget):
def __init__(self):
super().__init__()
self.ui = Ui_TestRegForm()
self.ui.setupUi(self)
#Calling browseImage
self.ui.browse_btn.clicked.connect(self.browseImage)
#Calling insertPers
self.ui.save_btn.clicked.connect(self.insertPers)
#browses person's picture and displays using label called image_label
def browseImage(self):
file_name = QFileDialog.getopenFileName(self,'Open File','c:\\','Image Files (*.png *.jpg *gif)')
image_path = file_name[0]
pixmap = Qpixmap(image_path)
self.ui.image_label.setpixmap(Qpixmap(pixmap))
#Inserts person into database
def insertPers(self):
try:
con = MysqLdb.connect(host="localhost",user="root",password="",database="somedb")
with con:
cur = con.cursor()
cur.execute("INSERT INTO persons(name,photo)" "VALUES('%s','%s')" % (''.join(self.ui.name_edit.text()),''.join(self.ui.image_label.text()))
con.commit()
self.displayDialog(QMessageBox.information,"Registration","Person has been added successfully")
except MysqLdb.Error as e:
self.displayDialog(QMessageBox.Warning,str(e))
except TypeError as e:
self.displayDialog(QMessageBox.Warning,str(e))
except ValueError as e:
self.displayDialog(QMessageBox.Warning,str(e))
解决方法
您不应连接变量以构建查询,而应使用占位符,否则您的代码将容易受到SQL Injection攻击的影响。另一方面,您必须使用QBuffer作为中介将QPixmap(而不是文本)转换为字节:
con = MySQLdb.connect(host="localhost",user="root",password="",database="somedb")
with con:
cur = con.cursor()
name = self.ui.name_edit.text()
buff = QBuffer()
buff.open(QIODevice.WriteOnly)
pixmap = QPixmap(self.ui.image_label.pixmap())
pixmap.save(buff,"PNG")
binary_img = buff.data().toBase64().data()
cur.execute("INSERT INTO persons(name,photo) VALUES (%s,%s)",(name,binary_img))
con.commit()