3

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.py", line 57, in AddEmployee
session = session.add(Employee(self.name,self.email))
AttributeError: 'bool' object has no attribute 'name'.

Can anyone help with a code snippet that can add information through a form created into database using 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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
rarevessell
  • 71
  • 2
  • 12

1 Answers1

2

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for this prompt answer.I am having this error after implementing your code."qlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 0 - probably unsupported type. [SQL: u'INSERT INTO employees (name, email) VALUES (?, ?)'] [parameters: (PyQt4.QtCore.QString(u'hi'), PyQt4.QtCore.QString(u'hi'))] (Background on this error at: http://sqlalche.me/e/rvf5) – rarevessell Nov 10 '18 at 19:17
  • 1
    Python2 or python3? – eyllanesc Nov 10 '18 at 19:20
  • Am using Python2.Thanks once again – rarevessell Nov 10 '18 at 19:21
  • @ eyllanesc,I will.Thanks – rarevessell Nov 10 '18 at 19:30
  • @rarevessell If my answer helps you do not forget to mark it as correct, if you do not know how to do it then review the [tour], that is the best way to thank. – eyllanesc Nov 10 '18 at 19:31
  • It did work as expected thanks ellanesc.Sorry,for the late response.I was having network issues – rarevessell Nov 11 '18 at 06:05
  • @ ellanesc,am trying to load the same data to a table.I will send my code soon.I need your help – rarevessell Nov 12 '18 at 08:42
  • ,Can you look into my code @ https://stackoverflow.com/questions/53276062/how-can-i-use-pyqt-to-load-data-from-database-via-sqlalchemy?I want to read from emloyee.db show into a table in PyQt – rarevessell Nov 14 '18 at 07:48