PyQt integration with Sqlalchemy











up vote
2
down vote

favorite












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









share|improve this question
























  • Thanks ellanesc
    – rarevessell
    Nov 10 at 18:24















up vote
2
down vote

favorite












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









share|improve this question
























  • Thanks ellanesc
    – rarevessell
    Nov 10 at 18:24













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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









share|improve this question















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






python sqlalchemy pyqt pyqt4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:47









eyllanesc

67.1k82952




67.1k82952










asked Nov 10 at 14:48









rarevessell

217




217












  • Thanks ellanesc
    – rarevessell
    Nov 10 at 18:24


















  • Thanks ellanesc
    – rarevessell
    Nov 10 at 18:24
















Thanks ellanesc
– rarevessell
Nov 10 at 18:24




Thanks ellanesc
– rarevessell
Nov 10 at 18:24












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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





share|improve this answer























  • 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: sqlalche.me/e/rvf5)
    – rarevessell
    Nov 10 at 19:17








  • 1




    Python2 or python3?
    – eyllanesc
    Nov 10 at 19:20










  • Am using Python2.Thanks once again
    – rarevessell
    Nov 10 at 19:21










  • @rarevessell I already update it, try my solution back :-)
    – eyllanesc
    Nov 10 at 19:27










  • @ eyllanesc,I will.Thanks
    – rarevessell
    Nov 10 at 19:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240087%2fpyqt-integration-with-sqlalchemy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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





share|improve this answer























  • 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: sqlalche.me/e/rvf5)
    – rarevessell
    Nov 10 at 19:17








  • 1




    Python2 or python3?
    – eyllanesc
    Nov 10 at 19:20










  • Am using Python2.Thanks once again
    – rarevessell
    Nov 10 at 19:21










  • @rarevessell I already update it, try my solution back :-)
    – eyllanesc
    Nov 10 at 19:27










  • @ eyllanesc,I will.Thanks
    – rarevessell
    Nov 10 at 19:30















up vote
1
down vote



accepted










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





share|improve this answer























  • 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: sqlalche.me/e/rvf5)
    – rarevessell
    Nov 10 at 19:17








  • 1




    Python2 or python3?
    – eyllanesc
    Nov 10 at 19:20










  • Am using Python2.Thanks once again
    – rarevessell
    Nov 10 at 19:21










  • @rarevessell I already update it, try my solution back :-)
    – eyllanesc
    Nov 10 at 19:27










  • @ eyllanesc,I will.Thanks
    – rarevessell
    Nov 10 at 19:30













up vote
1
down vote



accepted







up vote
1
down vote



accepted






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 19:27

























answered Nov 10 at 18:34









eyllanesc

67.1k82952




67.1k82952












  • 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: sqlalche.me/e/rvf5)
    – rarevessell
    Nov 10 at 19:17








  • 1




    Python2 or python3?
    – eyllanesc
    Nov 10 at 19:20










  • Am using Python2.Thanks once again
    – rarevessell
    Nov 10 at 19:21










  • @rarevessell I already update it, try my solution back :-)
    – eyllanesc
    Nov 10 at 19:27










  • @ eyllanesc,I will.Thanks
    – rarevessell
    Nov 10 at 19:30


















  • 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: sqlalche.me/e/rvf5)
    – rarevessell
    Nov 10 at 19:17








  • 1




    Python2 or python3?
    – eyllanesc
    Nov 10 at 19:20










  • Am using Python2.Thanks once again
    – rarevessell
    Nov 10 at 19:21










  • @rarevessell I already update it, try my solution back :-)
    – eyllanesc
    Nov 10 at 19:27










  • @ eyllanesc,I will.Thanks
    – rarevessell
    Nov 10 at 19:30
















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: sqlalche.me/e/rvf5)
– rarevessell
Nov 10 at 19:17






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: sqlalche.me/e/rvf5)
– rarevessell
Nov 10 at 19:17






1




1




Python2 or python3?
– eyllanesc
Nov 10 at 19:20




Python2 or python3?
– eyllanesc
Nov 10 at 19:20












Am using Python2.Thanks once again
– rarevessell
Nov 10 at 19:21




Am using Python2.Thanks once again
– rarevessell
Nov 10 at 19:21












@rarevessell I already update it, try my solution back :-)
– eyllanesc
Nov 10 at 19:27




@rarevessell I already update it, try my solution back :-)
– eyllanesc
Nov 10 at 19:27












@ eyllanesc,I will.Thanks
– rarevessell
Nov 10 at 19:30




@ eyllanesc,I will.Thanks
– rarevessell
Nov 10 at 19:30


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240087%2fpyqt-integration-with-sqlalchemy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation