尝试使用Python pyqt5在Mysql上插入数据

时间:2017-11-08 02:55:24

标签: python mysql pyqt5

我是Python的新手,我正在尝试做一件简单的事情。 我有两个字段标签(cod。客户端),需要从自动增量字段的Mysql接收select查询的结果,另一个需要在db上插入的字段editline(名称客户端)。 但我不知道问题出在哪里,你能帮忙吗?按照我的代码。

import MySQLdb
from PyQt5 import QtCore, QtGui, QtWidgets

conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="test")
c = conn.cursor()


class Ui_MainWindow(object):

    def select(self):
        c.execute("SELECT id FROM client")

    def insert(self):
        c.execute('INSERT INTO client(name) values("%s")' % (self.nameClient))
        conn.commit()
        conn.close()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(200, 200)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.labelCodClient = QtWidgets.QLabel(self.centralwidget, self.select())
        self.labelCodClient.setGeometry(QtCore.QRect(5, 10, 75, 23))
        self.labelCodClient.setObjectName("labelCodClient")

        self.labelNameClient = QtWidgets.QLabel("Name Client", self.centralwidget)
        self.labelNameClient.setGeometry(QtCore.QRect(5, 60, 75, 23))
        self.labelNameClient.setObjectName("labelNameClient")

        self.nameClient = QtWidgets.QLineEdit(self.centralwidget)
        self.nameClient.setGeometry(QtCore.QRect(75, 60, 75, 23))
        self.nameClient.setObjectName("nameClientInput")

        self.insertButton = QtWidgets.QPushButton(self.centralwidget)
        self.insertButton.setGeometry(QtCore.QRect(10, 100, 75, 23))
        self.insertButton.setObjectName("insertButton")
        self.insertButton.clicked.connect(self.insert())

        self.quitButton = QtWidgets.QPushButton(self.centralwidget)
        self.quitButton.setGeometry(QtCore.QRect(95, 100, 75, 23))
        self.quitButton.setObjectName("quitButton")
        self.quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)

        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.quitButton.setText(_translate("MainWindow", "Quit"))
        self.insertButton.setText(_translate("MainWindow", "Insert"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

0 个答案:

没有答案