将图像从tableWidget显示为PyQt5形式

问题描述

图像已在表格小部件列中显示为图标。我现在试图展示一种搜索功能,该功能将接受行编辑,以便可以通过某些标签显示查询的行数据。如果我的查询格式容易受到SQL注入的影响,请按照最佳实践帮助检查和修改此代码,并帮助进行更正,以使我能够在表单上显示从数据库中获取的图像。该代码仅用于显示文本数据,但我没有重新显示从tablewidget提取的图像的逻辑。

 def searchPers(self):
    con = MySQLdb.connect(host="localhost",user="root",password="",database="db")
    with con:
        cur = con.cursor()

        name = self.ui.name_edit.text()

        cmd = "SELECT * FROM persons WHERE name LIKE'"+name+"'"
        cur.execute(cmd)
        row = cur.fetchone()

        if row == None:
            self.ui.response_label.setText("Person not found")
        else:
            self.ui.name_label.setText(str(row[0]))
            pixmap = QPixmap()
            pixmap.loadFromData(QByteArray.toBase64(row[1]))
            self.ui.photo_label.setPixmap(QPixmap(pixmap))

解决方法

经过几次试验和错误,我后来知道了:

def searchPers(self):
  con = MySQLdb.connect(host="localhost",user="root",password="",database="db")
  with con:
    cur = con.cursor()

    name = self.ui.name_edit.text()

    cmd = "SELECT * FROM persons WHERE name LIKE'"+name+"'"
    cur.execute(cmd)
    row = cur.fetchone()

    if row == None:
        self.ui.response_label.setText("Person not found")
    else:
        self.ui.name_label.setText(str(row[0]))
        pixmap = QPixmap()
        pixmap.loadFromData(QByteArray.fromBase64(row[1]))
        self.ui.photo_label.setPixmap(QPixmap(pixmap))

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...