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_())
python sqlalchemy pyqt pyqt4
add a comment |
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_())
python sqlalchemy pyqt pyqt4
Thanks ellanesc
– rarevessell
Nov 10 at 18:24
add a comment |
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_())
python sqlalchemy pyqt pyqt4
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
python sqlalchemy pyqt pyqt4
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
add a comment |
Thanks ellanesc
– rarevessell
Nov 10 at 18:24
Thanks ellanesc
– rarevessell
Nov 10 at 18:24
Thanks ellanesc
– rarevessell
Nov 10 at 18:24
add a comment |
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_())
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
|
show 5 more comments
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_())
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
|
show 5 more comments
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_())
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
|
show 5 more comments
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_())
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_())
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
|
show 5 more comments
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
|
show 5 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Thanks ellanesc
– rarevessell
Nov 10 at 18:24