以下是一个简单的使用 Python 和 Qt 制作增删改查 MySQL 表的示例界面。该表包含姓名和案件名称两个字段。

您需要安装 PyQt5 和 MySQL Python Connector 才能运行此代码。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QTableView, QMessageBox
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('MySQL 操作')
        self.setGeometry(100, 100, 800, 600)
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        # 连接 MySQL 数据库
        self.db = QSqlDatabase.addDatabase('QMYSQL')
        self.db.setHostName('localhost')
        self.db.setUserName('root')
        self.db.setPassword('password')
        self.db.setDatabaseName('test_db')

        if not self.db.open():
            QMessageBox.critical(self, '错误', '无法连接到数据库')
            return

        # 建立模型
        self.model = QSqlTableModel()
        self.model.setTable('test_table')
        self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
        self.model.select()

        # 建立视图
        self.view = QTableView()
        self.view.setModel(self.model)

        # 建立输入框
        self.name_input = QLineEdit()
        self.case_input = QLineEdit()

        # 建立标签
        self.name_label = QLabel('姓名')
        self.case_label = QLabel('案件名称')

        # 建立按钮
        self.add_button = QPushButton('添加')
        self.del_button = QPushButton('删除')
        self.update_button = QPushButton('更新')

        # 布局
        self.input_layout = QHBoxLayout()
        self.input_layout.addWidget(self.name_label)
        self.input_layout.addWidget(self.name_input)
        self.input_layout.addWidget(self.case_label)
        self.input_layout.addWidget(self.case_input)

        self.button_layout = QHBoxLayout()
        self.button_layout.addWidget(self.add_button)
        self.button_layout.addWidget(self.del_button)
        self.button_layout.addWidget(self.update_button)

        self.main_layout = QVBoxLayout()
        self.main_layout.addLayout(self.input_layout)
        self.main_layout.addLayout(self.button_layout)
        self.main_layout.addWidget(self.view)

        self.central_widget.setLayout(self.main_layout)

        # 按钮点击事件
        self.add_button.clicked.connect(self.add_record)
        self.del_button.clicked.connect(self.del_record)
        self.update_button.clicked.connect(self.update_record)

    # 添加记录
    def add_record(self):
        name = self.name_input.text()
        case = self.case_input.text()

        if name and case:
            self.model.insertRows(self.model.rowCount(), 1)
            self.model.setData(self.model.index(self.model.rowCount() - 1, 0), name)
            self.model.setData(self.model.index(self.model.rowCount() - 1, 1), case)
            self.model.submitAll()
        else:
            QMessageBox.warning(self, '错误', '姓名和案件名称不能为空')

    # 删除记录
    def del_record(self):
        selected = self.view.selectedIndexes()

        if selected:
            rows = []

            for idx in selected:
                row = idx.row()

                if row not in rows:
                    rows.append(row)

            rows.sort(reverse=True)

            for row in rows:
                self.model.removeRow(row)

            self.model.submitAll()
        else:
            QMessageBox.warning(self, '错误', '请选择要删除的记录')

    # 更新记录
    def update_record(self):
        selected = self.view.selectedIndexes()

        if selected:
            name = self.name_input.text()
            case = self.case_input.text()

            for idx in selected:
                row = idx.row()
                self.model.setData(self.model.index(row, 0), name)
                self.model.setData(self.model.index(row, 1), case)

            self.model.submitAll()
        else:
            QMessageBox.warning(self, '错误', '请选择要更新的记录')

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

在此示例中,我们使用了 QSqlTableModel 和 QTableView 来显示 MySQL 表中的数据。我们还使用了 QLineEdit、QLabel 和 QPushButton 来创建用户输入和按钮。用户可以添加、删除和更新表中的记录。如果连接到 MySQL 数据库失败,系统会弹出一个错误消息框。我们还使用 QMessageBox 来显示警告消息框,以便用户了解错误和警告消息。

运行此代码后,您将看到一个包含姓名和案件名称字段的表,以及三个按钮:添加、删除和更新。您可以使用这些按钮来操作表中的数据。

Python Qt 增删改查 MySQL 表:姓名与案件名称

原文地址: https://www.cveoy.top/t/topic/offh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录