PyQt与Sqlalchemy集成

时间:2018-11-10 14:48:08

标签: python sqlalchemy pyqt pyqt4

我正在尝试通过Sqlalchemy将我用PyQt创建的表单添加到数据库中,但我猜我的代码有问题。我收到此错误

le "/Users/tunji/Desktop/employee.py", line 57, in AddEmployee
session = session.add(Employee(self.name,self.email))
AttributeError: 'bool' object has no attribute 'name'.

任何人都可以帮助一个代码片段,该代码片段可以使用Sqlalchemy通过在数据库中创建的表单添加信息吗?

from PyQt4 import QtCore, QtGui

import sqlalchemy 
from sqlalchemy.ext.declarative import declarative_base

# My base structure

Base = declarative_base()


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Employee(Base):
    __tablename__   = 'employees'   
    employee_id     = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)

    name            = sqlalchemy.Column(sqlalchemy.String, nullable=False)

    email           = sqlalchemy.Column(sqlalchemy.String)

    def __repr__(self):
       return "<User(name='%s', email='%s')>" % (self.name, self.email)

engine = sqlalchemy.create_engine("sqlite:///my_db.db", echo='debug')
Base.metadata.create_all(engine)
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()



class Ui_Dialog(object):
    DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
    session = DBsession()

    def AddEmployee(Base,self):
        session = DBsession()
        session = session.add(Employee(self.name,self.email))
        session.commit()

    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 276)
        self.name_lineEdit = QtGui.QLineEdit(Dialog)
        self.name_lineEdit.setGeometry(QtCore.QRect(180, 30, 113, 21))
        self.name_lineEdit.setObjectName(_fromUtf8("name_lineEdit"))
        self.email_lineEdit = QtGui.QLineEdit(Dialog)
        self.email_lineEdit.setGeometry(QtCore.QRect(180, 90, 113, 21))
        self.email_lineEdit.setObjectName(_fromUtf8("email_lineEdit"))
        self.name_label = QtGui.QLabel(Dialog)
        self.name_label.setGeometry(QtCore.QRect(80, 30, 60, 16))
        self.name_label.setObjectName(_fromUtf8("name_label"))
        self.email_label = QtGui.QLabel(Dialog)
        self.email_label.setGeometry(QtCore.QRect(80, 90, 60, 16))
        self.email_label.setObjectName(_fromUtf8("email_label"))
        self.add_employee_btn = QtGui.QPushButton(Dialog)
        self.add_employee_btn.setGeometry(QtCore.QRect(180, 170, 113, 32))
        self.add_employee_btn.setObjectName(_fromUtf8("add_employee_btn"))
        self.add_employee_btn.clicked.connect(self.AddEmployee)

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Add Employee", None))
        self.name_label.setText(_translate("Dialog", "Name", None))
        self.email_label.setText(_translate("Dialog", "email", None))
        self.add_employee_btn.setText(_translate("Dialog", "AddEmployee", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

首先,您必须了解自我的目标是什么,并且自我在方法中的位置不只是任何人(因为它读为What is the purpose of self?),例如在def AddEmployee(Base, self)中,为什么您认为AddEmployee通过了Base?,这没有意义,self必须是第一个to参数,而您添加的另一个参数将是Boolean,因为单击默认值是参数。

另一方面,PyQt建议不要修改设计,而要创建另一个类,该类继承适当的小部件并用设计填充(有关更多信息,请阅读http://pyqt.sourceforge.net/Docs/PyQt5/designer.html)。

另一个错误是您必须学会重用而不是不必要地创建。另一个错误是session.add()不返回任何内容。

考虑并纠正上述错误,可以得到以下信息:

from PyQt4 import QtCore, QtGui

import sqlalchemy 
from sqlalchemy.ext.declarative import declarative_base


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 276)
        self.name_lineEdit = QtGui.QLineEdit(Dialog)
        self.name_lineEdit.setGeometry(QtCore.QRect(180, 30, 113, 21))
        self.name_lineEdit.setObjectName(_fromUtf8("name_lineEdit"))
        self.email_lineEdit = QtGui.QLineEdit(Dialog)
        self.email_lineEdit.setGeometry(QtCore.QRect(180, 90, 113, 21))
        self.email_lineEdit.setObjectName(_fromUtf8("email_lineEdit"))
        self.name_label = QtGui.QLabel(Dialog)
        self.name_label.setGeometry(QtCore.QRect(80, 30, 60, 16))
        self.name_label.setObjectName(_fromUtf8("name_label"))
        self.email_label = QtGui.QLabel(Dialog)
        self.email_label.setGeometry(QtCore.QRect(80, 90, 60, 16))
        self.email_label.setObjectName(_fromUtf8("email_label"))
        self.add_employee_btn = QtGui.QPushButton(Dialog)
        self.add_employee_btn.setGeometry(QtCore.QRect(180, 170, 113, 32))
        self.add_employee_btn.setObjectName(_fromUtf8("add_employee_btn"))

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Add Employee", None))
        self.name_label.setText(_translate("Dialog", "Name", None))
        self.email_label.setText(_translate("Dialog", "email", None))
        self.add_employee_btn.setText(_translate("Dialog", "AddEmployee", None))


Base = declarative_base()

class Employee(Base):
    __tablename__   = 'employees'   
    employee_id     = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    name            = sqlalchemy.Column(sqlalchemy.String, nullable=False)
    email           = sqlalchemy.Column(sqlalchemy.String)

    def __repr__(self):
       return "<User(name='%s', email='%s')>" % (self.name, self.email)

engine = sqlalchemy.create_engine("sqlite:///my_db.db", echo='debug')
Base.metadata.create_all(engine)
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()

class Dialog(QtGui.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        self.add_employee_btn.clicked.connect(self.addEmployee)

    @QtCore.pyqtSlot()
    def addEmployee(self):
        name = str(self.name_lineEdit.text())
        email = str(self.email_lineEdit.text())
        session.add(Employee(name=name, email=email))
        session.commit()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())