拒绝SQL恐惧:用Python+pyqt打造任意Excel数据库查询系统

一、引言

在数字化转型浪潮中,超过76%的基层业务人员仍被困在"SQL恐惧症"的泥潭里------他们精通业务逻辑却受限于技术门槛,面对海量数据时只能反复请求IT部门协助。本项目通过Python+PyQt来构建基于Excel风格的查询系统,从而打破这种低效循环:PyQt5提供直观界面可视化组件,pandas库实现"无SQL"的数据操作转换,对用户上传的任意电子表格Excel文件均可完成复杂数据库查询

二、GUI界面设计

使用PyQt5进行界面的搭建,最终界面如下:

1.第一步:上传任意Excel文件

直接点击上传即可。

这里可选择上传任意填写好的Excel文件,但需要注意

①Excel文件是纯数据文件不能包含表头、标题行、合并单元格等特殊格式

②Excel中的数据量不受限 ,但要求第一列必须为查找的主键值****(主键值是指数据库表中主键字段(或字段组合)所存储的具体数值或字符串,用于唯一标识表中的每一行记录,确保数据可唯一识别和访问),其余列可填写对应数据。

以成绩表作为演示示例:如若想查询全校所有人的各科成绩信息,则第一列应为该学生的学号(主键),其余各列可放置该学生的全部科目成绩。

2.第二步:选择查询字段

在第一步上传任意Excel成功后,会自动根据上传文件更新"请选择主键字段"和"请选择待查询字段"内的选项 ,并且在"请选择主键字段"这一选项下方出现输入框方便用户输入主键内容 。对于"请选择主键字段"这一选项,在第一步中已明确规定:Excel中第一列为主键字段,所以这里仅额外增加一个选项(以成绩表为例,这里自动增加"学号"字段) ;而对于"请选择待查询字段"这一选项,会将Excel中除第一列以外的所有列名均作为新增选项(以成绩表为例,这里自动增加"语文"、"数学"、"英语"等其他所有字段),效果如下:

3.第三步:输入主键值并查询

选择好主键字段及待查询字段,并输入待查询主键值后,点击"开始查询"即可。

最终查询效果如下:

可以看到,结果被分为两个区域左边表示查询结果 (即学生姓名),右边表示与查询结果相关的其他所有Excel数据(即除姓名和学号之外,该生的所有成绩信息)。

4.异常提示

若未进行相应的操作,跳过某个步骤或直接点击"开始查询",则会出现以下的异常提示。

5.界面设计jiemian.py

最后附上通过pyuic5产生的GUI界面代码jiemian.py

python 复制代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'jiemian.ui'
#
# Created by: PyQt5 UI code generator 5.15.11
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.setEnabled(True)
        Form.resize(600, 500)
        Form.setMinimumSize(QtCore.QSize(600, 500))
        Form.setMaximumSize(QtCore.QSize(600, 500))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/image1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Form.setWindowIcon(icon)
        self.label_5 = QtWidgets.QLabel(Form)
        self.label_5.setGeometry(QtCore.QRect(390, 50, 141, 31))
        font = QtGui.QFont()
        font.setFamily("Adobe Arabic")
        font.setPointSize(16)
        self.label_5.setFont(font)
        self.label_5.setObjectName("label_5")
        self.label_8 = QtWidgets.QLabel(Form)
        self.label_8.setGeometry(QtCore.QRect(220, 20, 231, 31))
        font = QtGui.QFont()
        font.setFamily("Adobe Arabic")
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.label_8.setFont(font)
        self.label_8.setObjectName("label_8")
        self.label_10 = QtWidgets.QLabel(Form)
        self.label_10.setGeometry(QtCore.QRect(470, 40, 71, 51))
        self.label_10.setText("")
        self.label_10.setPixmap(QtGui.QPixmap(":/image1.png"))
        self.label_10.setObjectName("label_10")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(80, 90, 161, 21))
        font = QtGui.QFont()
        font.setFamily("Adobe Arabic")
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(120, 120, 161, 23))
        self.pushButton.setObjectName("pushButton")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(80, 170, 161, 21))
        font = QtGui.QFont()
        font.setFamily("Adobe Arabic")
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.comboBox = QtWidgets.QComboBox(Form)
        self.comboBox.setGeometry(QtCore.QRect(120, 200, 161, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox_2 = QtWidgets.QComboBox(Form)
        self.comboBox_2.setGeometry(QtCore.QRect(330, 200, 161, 22))
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(80, 300, 221, 151))
        font = QtGui.QFont()
        font.setFamily("Adobe Arabic")
        font.setPointSize(28)
        self.label_3.setFont(font)
        self.label_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.label_3.setText("")
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(80, 470, 471, 23))
        self.pushButton_3.setObjectName("pushButton_3")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(120, 250, 161, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.textEdit = QtWidgets.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(320, 300, 231, 151))
        self.textEdit.setObjectName("textEdit")
        self.label_4 = QtWidgets.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(80, 280, 161, 16))
        self.label_4.setObjectName("label_4")
        self.label_6 = QtWidgets.QLabel(Form)
        self.label_6.setGeometry(QtCore.QRect(320, 280, 161, 16))
        self.label_6.setObjectName("label_6")
        self.label_7 = QtWidgets.QLabel(Form)
        self.label_7.setGeometry(QtCore.QRect(120, 230, 161, 16))
        self.label_7.setObjectName("label_7")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Kebiao_query"))
        self.label_5.setText(_translate("Form", "Designed By"))
        self.label_8.setText(_translate("Form", "数据库查询系统"))
        self.label.setText(_translate("Form", "一、上传数据文件"))
        self.pushButton.setText(_translate("Form", "点击上传"))
        self.label_2.setText(_translate("Form", "二、选择查询字段"))
        self.comboBox.setItemText(0, _translate("Form", "请选择主键字段"))
        self.comboBox_2.setItemText(0, _translate("Form", "请选择待查询字段"))
        self.pushButton_3.setText(_translate("Form", "开始查询"))
        self.label_4.setText(_translate("Form", "查询结果如下:"))
        self.label_6.setText(_translate("Form", "相关查询结果如下:"))
        self.label_7.setText(_translate("Form", "请输入主键值:"))
import ziyuan_rc

三、主要程序详解

1.导入所需模块

python 复制代码
import sys
from jiemian import *
from PyQt5.QtWidgets import QApplication, QWidget
import pandas as pd
# 保持窗口大小和qtdesigner中的一致
from PyQt5 import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

不懂为啥需要导入Qtcore的,请看****************************一键曝光:Python+PyQt实现的文件目录透视镜****************************

2.初始化设置

python 复制代码
    def __init__(self):
        super(QWidget, self).__init__()
        self.setupUi(self)

        self.pushButton.clicked.connect(self.shangchuan_wenjian)
        self.pushButton_3.clicked.connect(self.kaishi)
        self.lineEdit.setVisible(False)
        self.label_4.setVisible(False)
        self.label_6.setVisible(False)
        self.label_7.setVisible(False)

        self.shangchuan_flag = False

主要将pushbutton绑定于对应的信号函数,并对一些lineedit和label设置visible效果,目的是只有触发某些动作或函数时,才将其显示状态更改为True,否则一直不显示(即False状态);shangchuan_flag 作为是否上传任意Excel表的标志位。

3.上传文件并更新选项

python 复制代码
    def shangchuan_wenjian(self):
        self.wenjian_df = shangchuan()
        if not self.wenjian_df.empty:
            self.shangchuan_flag = True
            self.lineEdit.setVisible(True)
            self.label_7.setVisible(True)

            self.columns = self.wenjian_df.columns.tolist()
            self.comboBox.clear()
            self.comboBox.addItem("请选择主键字段")
            self.comboBox.addItem(self.columns[0])
            self.comboBox_2.clear()
            self.comboBox_2.addItem("请选择待查询字段")
            self.comboBox_2.addItems(self.columns[1:])

调用自定义函数shangchuan 来实现弹窗效果;当文件内容不为空时,设置上传标志位shangchuan_flag为True,并显示lineedit(主键值输入框)和label_7("请输入主键值");读取所有文件列名并存储在列表columns中,将列表columns中的第一个值(即第一列的列名)添加至combobox中,其余值添加至combobox_2中。但需要注意:每次添加item(s)前,需要将其原有选项清空clear,否则容易造成选项堆叠重复。

4.查询信息并显示

python 复制代码
    def kaishi(self):
        if self.shangchuan_flag==True:
            if self.comboBox.currentIndex()!=0:
                if self.comboBox_2.currentIndex()!=0:

                    lieziduan = self.comboBox_2.currentText()
                    liesuoyin = self.wenjian_df.columns.get_loc(lieziduan)

                    # 根据行字段确定行索引
                    hangsuoyin = -1
                    for i in range(0, len(self.wenjian_df)):
                        if str(self.wenjian_df.iloc[i,0]) == self.lineEdit.text():
                            hangsuoyin = i
                            break

                    chaxunzhi = self.wenjian_df.iloc[hangsuoyin, liesuoyin]

                    # 匹配剩余未查询值
                    ewai_lst = []
                    for column_name in self.columns:
                        new_column_index = self.wenjian_df.columns.get_loc(column_name)
                        if (new_column_index != liesuoyin) and (new_column_index != 0):
                            ewaizhi = self.wenjian_df.iloc[hangsuoyin, new_column_index]
                            ewai_lst.append(column_name+":"+str(ewaizhi))

                    if pd.isna(chaxunzhi) or hangsuoyin==-1:
                        self.label_4.setVisible(True)
                        self.label_6.setVisible(True)
                        self.label_3.setText("未找到")
                        self.textEdit.setText("未找到")
                    else:
                        self.label_4.setVisible(True)
                        self.label_6.setVisible(True)
                        self.label_3.setText(str(chaxunzhi))
                        self.textEdit.setText("\n".join(ewai_lst))

                else:
                    QtWidgets.QMessageBox.critical(self, "提示", "请选择主键字段!")
            else:
                QtWidgets.QMessageBox.critical(self, "提示", "请选择待查询字段!")
        else:
            QtWidgets.QMessageBox.critical(self, "提示", "请检查操作步骤或上传文件!")

以shangchaun_flag作为标志位,检查是否上传模板文件;依次判断是否选择每个combobox里的对应选项;若均满足,则根据combobox_2的内容定位列字段名称,再根据列字段名称反推列索引;根据lineedit内容通过循环查询对应的值,找到后即可退出循环,避免运算复杂度,最终得到行索引最后通过得到的行索引和列索引确定查询值,并通过label_3显示 ;剩余未查询值,思路方法也是同理,最终显示在textedit中。若遇到查询不到的情况 时,显示的内容均设置为"未找到"。当然,哪个combobox有问题,则显示不同的对应提示。

5.自定义函数

python 复制代码
def shangchuan():
    filepath, _ = QtWidgets.QFileDialog.getOpenFileName(None, "请选择文件", "", "XLSX工作表 (*.xlsx)")  # 获取文件路径
    if filepath:
        # 获取原始df
        yuanshi_df = pd.read_excel(filepath)
        if not yuanshi_df.empty:
            QtWidgets.QMessageBox.information(None, "成功", "上传成功!")
            return yuanshi_df
        else:
            QtWidgets.QMessageBox.critical(None, "提示", "请检查操作步骤或上传文件!")
    else:
        QtWidgets.QMessageBox.critical(None, "提示", "请选择XLSX工作表类型!")
        return pd.DataFrame()

一旦需要上传文件时,直接调用此函数即可。

四、总程序代码Kebiao_query.py

python 复制代码
import sys
from jiemian import *
from PyQt5.QtWidgets import QApplication, QWidget
import pandas as pd
# 保持窗口大小和qtdesigner中的一致
from PyQt5 import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

class mainwindow(QWidget, Ui_Form):
    def __init__(self):
        super(QWidget, self).__init__()
        self.setupUi(self)

        self.pushButton.clicked.connect(self.shangchuan_wenjian)
        self.pushButton_3.clicked.connect(self.kaishi)
        self.lineEdit.setVisible(False)
        self.label_4.setVisible(False)
        self.label_6.setVisible(False)
        self.label_7.setVisible(False)

        self.shangchuan_flag = False

    def shangchuan_wenjian(self):
        self.wenjian_df = shangchuan()
        if not self.wenjian_df.empty:
            self.shangchuan_flag = True
            self.lineEdit.setVisible(True)
            self.label_7.setVisible(True)

            self.columns = self.wenjian_df.columns.tolist()
            self.comboBox.clear()
            self.comboBox.addItem("请选择主键字段")
            self.comboBox.addItem(self.columns[0])
            self.comboBox_2.clear()
            self.comboBox_2.addItem("请选择待查询字段")
            self.comboBox_2.addItems(self.columns[1:])

    def kaishi(self):
        if self.shangchuan_flag==True:
            if self.comboBox.currentIndex()!=0:
                if self.comboBox_2.currentIndex()!=0:

                    lieziduan = self.comboBox_2.currentText()
                    liesuoyin = self.wenjian_df.columns.get_loc(lieziduan)

                    # 根据行字段确定行索引
                    hangsuoyin = -1
                    for i in range(0, len(self.wenjian_df)):
                        if str(self.wenjian_df.iloc[i,0]) == self.lineEdit.text():
                            hangsuoyin = i
                            break

                    chaxunzhi = self.wenjian_df.iloc[hangsuoyin, liesuoyin]

                    # 匹配剩余未查询值
                    ewai_lst = []
                    for column_name in self.columns:
                        new_column_index = self.wenjian_df.columns.get_loc(column_name)
                        if (new_column_index != liesuoyin) and (new_column_index != 0):
                            ewaizhi = self.wenjian_df.iloc[hangsuoyin, new_column_index]
                            ewai_lst.append(column_name+":"+str(ewaizhi))

                    if pd.isna(chaxunzhi) or hangsuoyin==-1:
                        self.label_4.setVisible(True)
                        self.label_6.setVisible(True)
                        self.label_3.setText("未找到")
                        self.textEdit.setText("未找到")
                    else:
                        self.label_4.setVisible(True)
                        self.label_6.setVisible(True)
                        self.label_3.setText(str(chaxunzhi))
                        self.textEdit.setText("\n".join(ewai_lst))

                else:
                    QtWidgets.QMessageBox.critical(self, "提示", "请选择主键字段!")
            else:
                QtWidgets.QMessageBox.critical(self, "提示", "请选择待查询字段!")
        else:
            QtWidgets.QMessageBox.critical(self, "提示", "请检查操作步骤或上传文件!")

def shangchuan():
    filepath, _ = QtWidgets.QFileDialog.getOpenFileName(None, "请选择文件", "", "XLSX工作表 (*.xlsx)")  # 获取文件路径
    if filepath:
        # 获取原始df
        yuanshi_df = pd.read_excel(filepath)
        if not yuanshi_df.empty:
            QtWidgets.QMessageBox.information(None, "成功", "上传成功!")
            return yuanshi_df
        else:
            QtWidgets.QMessageBox.critical(None, "提示", "请检查操作步骤或上传文件!")
    else:
        QtWidgets.QMessageBox.critical(None, "提示", "请选择XLSX工作表类型!")
        return pd.DataFrame()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = mainwindow()
    w.show()
    sys.exit(app.exec_())

仅以成绩表作为示例,上传其他任意Excel均可!

欢迎留言/私信沟通交流!

相关推荐
seasonsyy6 分钟前
1.安装anaconda详细步骤(含安装截图)
python·深度学习·环境配置
♡喜欢做梦19 分钟前
【MySQL】深入浅出事务:保证数据一致性的核心武器
数据库·mysql
遇见你的雩风22 分钟前
MySQL的认识与基本操作
数据库·mysql
半新半旧25 分钟前
python 整合使用 Redis
redis·python·bootstrap
dblens 数据库管理和开发工具25 分钟前
MySQL新增字段DDL:锁表全解析、避坑指南与实战案例
数据库·mysql·dblens·dblens mysql·数据库连接管理
weixin_4196583126 分钟前
MySQL的基础操作
数据库·mysql
Blossom.1181 小时前
基于深度学习的图像分类:使用Capsule Networks实现高效分类
人工智能·python·深度学习·神经网络·机器学习·分类·数据挖掘
CodeCraft Studio1 小时前
借助Aspose.HTML控件,在 Python 中将 HTML 转换为 Markdown
开发语言·python·html·markdown·aspose·html转markdown·asposel.html
悠哉悠哉愿意1 小时前
【电赛学习笔记】MaxiCAM 项目实践——与单片机的串口通信
笔记·python·单片机·嵌入式硬件·学习·视觉检测
封奚泽优1 小时前
使用Python实现单词记忆软件
开发语言·python·random·qpushbutton·qtwidgets·qtcore·qtgui