Skip to content Skip to sidebar Skip to footer

Pyqt Integration With Sqlalchemy

I am trying to add a form I created with PyQt into database through Sqlalchemy but I guess something is wrong with my code.I am getting this error le '/Users/tunji/Desktop/employee

Solution 1:

First of all you must understand what is the goal of self and that its position within the methods is not just anybody (for that it reads What is the purpose of self?), for example in def AddEmployee(Base, self), on the other hand, why do you think that AddEmployee pass the Base?, that does not make sense, the self must be the first to parameter, and your add another parameter this will be a Boolean because clicked to default that parameter.

On the other hand PyQt recommend not to modify the design but to create another class that inherits the appropriate widget and fill it with the design (for more information reads http://pyqt.sourceforge.net/Docs/PyQt5/designer.html).

Another mistake is that you must learn to reuse instead of create unnecessarily. Another error is that session.add() does not return anything.

Considering and correcting the errors indicated above, the following is obtained:

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_())

Post a Comment for "Pyqt Integration With Sqlalchemy"