How can I read data from database and show it in a PyQt table
I am trying to load data from database that I added to the database through this code PyQt integration with Sqlalchemy .I want the data from the database to be displayed into a table.I have tried this code.But,I am not getting it to load the data appropriately.I think the SqlAlchemy query may be wrong.
Any help will be appreciated.
I am getting this error:
File "/Users/tunji/Desktop/plain.py", line 75, in load
result = session(Employee).query.all()
NameError: global name 'Employee
but when I updated this line of code:
from employee import Employee
I am now having this error:
Traceback (most recent call last): File "plain.py", line 82, in load
for column_number,data in enumerate(row_data): TypeError: 'Employee' object is not iterable
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(220, 120, 256, 192))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(310, 390, 113, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
#esult = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session(Employee).query.all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
python sqlalchemy pyqt
add a comment |
I am trying to load data from database that I added to the database through this code PyQt integration with Sqlalchemy .I want the data from the database to be displayed into a table.I have tried this code.But,I am not getting it to load the data appropriately.I think the SqlAlchemy query may be wrong.
Any help will be appreciated.
I am getting this error:
File "/Users/tunji/Desktop/plain.py", line 75, in load
result = session(Employee).query.all()
NameError: global name 'Employee
but when I updated this line of code:
from employee import Employee
I am now having this error:
Traceback (most recent call last): File "plain.py", line 82, in load
for column_number,data in enumerate(row_data): TypeError: 'Employee' object is not iterable
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(220, 120, 256, 192))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(310, 390, 113, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
#esult = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session(Employee).query.all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
python sqlalchemy pyqt
This line in theMainWindow.load()
method:result = session(Employee).query.all()
. The variableEmployee
is not defined anywhere in your module. Either you need to define theEmployee
model in you module, or import it from somewhere.
– SuperShoot
Nov 13 '18 at 9:10
I will do that,thanks for the help
– rarevessell
Nov 13 '18 at 9:28
@ IIja Everila and SuperShoot,can you help with the code?
– rarevessell
Nov 13 '18 at 19:25
add a comment |
I am trying to load data from database that I added to the database through this code PyQt integration with Sqlalchemy .I want the data from the database to be displayed into a table.I have tried this code.But,I am not getting it to load the data appropriately.I think the SqlAlchemy query may be wrong.
Any help will be appreciated.
I am getting this error:
File "/Users/tunji/Desktop/plain.py", line 75, in load
result = session(Employee).query.all()
NameError: global name 'Employee
but when I updated this line of code:
from employee import Employee
I am now having this error:
Traceback (most recent call last): File "plain.py", line 82, in load
for column_number,data in enumerate(row_data): TypeError: 'Employee' object is not iterable
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(220, 120, 256, 192))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(310, 390, 113, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
#esult = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session(Employee).query.all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
python sqlalchemy pyqt
I am trying to load data from database that I added to the database through this code PyQt integration with Sqlalchemy .I want the data from the database to be displayed into a table.I have tried this code.But,I am not getting it to load the data appropriately.I think the SqlAlchemy query may be wrong.
Any help will be appreciated.
I am getting this error:
File "/Users/tunji/Desktop/plain.py", line 75, in load
result = session(Employee).query.all()
NameError: global name 'Employee
but when I updated this line of code:
from employee import Employee
I am now having this error:
Traceback (most recent call last): File "plain.py", line 82, in load
for column_number,data in enumerate(row_data): TypeError: 'Employee' object is not iterable
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(220, 120, 256, 192))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(310, 390, 113, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
#esult = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session(Employee).query.all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
python sqlalchemy pyqt
python sqlalchemy pyqt
edited Nov 14 '18 at 7:53
rarevessell
asked Nov 13 '18 at 7:40
rarevessellrarevessell
2118
2118
This line in theMainWindow.load()
method:result = session(Employee).query.all()
. The variableEmployee
is not defined anywhere in your module. Either you need to define theEmployee
model in you module, or import it from somewhere.
– SuperShoot
Nov 13 '18 at 9:10
I will do that,thanks for the help
– rarevessell
Nov 13 '18 at 9:28
@ IIja Everila and SuperShoot,can you help with the code?
– rarevessell
Nov 13 '18 at 19:25
add a comment |
This line in theMainWindow.load()
method:result = session(Employee).query.all()
. The variableEmployee
is not defined anywhere in your module. Either you need to define theEmployee
model in you module, or import it from somewhere.
– SuperShoot
Nov 13 '18 at 9:10
I will do that,thanks for the help
– rarevessell
Nov 13 '18 at 9:28
@ IIja Everila and SuperShoot,can you help with the code?
– rarevessell
Nov 13 '18 at 19:25
This line in the
MainWindow.load()
method: result = session(Employee).query.all()
. The variable Employee
is not defined anywhere in your module. Either you need to define the Employee
model in you module, or import it from somewhere.– SuperShoot
Nov 13 '18 at 9:10
This line in the
MainWindow.load()
method: result = session(Employee).query.all()
. The variable Employee
is not defined anywhere in your module. Either you need to define the Employee
model in you module, or import it from somewhere.– SuperShoot
Nov 13 '18 at 9:10
I will do that,thanks for the help
– rarevessell
Nov 13 '18 at 9:28
I will do that,thanks for the help
– rarevessell
Nov 13 '18 at 9:28
@ IIja Everila and SuperShoot,can you help with the code?
– rarevessell
Nov 13 '18 at 19:25
@ IIja Everila and SuperShoot,can you help with the code?
– rarevessell
Nov 13 '18 at 19:25
add a comment |
1 Answer
1
active
oldest
votes
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
from employee import Employee
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(100, 300, 700,300))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(360, 650, 200, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Display Database", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
employees = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session.query(employees).all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53276062%2fhow-can-i-read-data-from-database-and-show-it-in-a-pyqt-table%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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
from employee import Employee
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(100, 300, 700,300))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(360, 650, 200, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Display Database", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
employees = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session.query(employees).all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
add a comment |
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
from employee import Employee
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(100, 300, 700,300))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(360, 650, 200, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Display Database", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
employees = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session.query(employees).all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
add a comment |
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
from employee import Employee
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(100, 300, 700,300))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(360, 650, 200, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Display Database", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
employees = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session.query(employees).all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'plain.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, MetaData,Table
from sqlalchemy.orm import mapper,Session
from employee import Employee
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 576)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(100, 300, 700,300))
self.tableWidget.setRowCount(15)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.btn_load = QtGui.QPushButton(self.centralwidget)
self.btn_load.setGeometry(QtCore.QRect(360, 650, 200, 32))
self.btn_load.setObjectName(_fromUtf8("btn_load"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Display Database", None))
self.btn_load.setText(_translate("MainWindow", "Load", None))
Base = declarative_base()
engine = sqlalchemy.create_engine("sqlite:///employee.db", echo='debug')
Base.metadata.create_all(engine)
metadata = MetaData()
DBsession = sqlalchemy.orm.sessionmaker(bind=engine)
session = DBsession()
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_load.clicked.connect(self.load)
@QtCore.pyqtSlot()
def load(self):
employees = Table('employees',metadata,autoload=True,autoload_with=engine)
result = session.query(employees).all()
self.tableWidget.setRowCount(0)
for row_number,row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtGui.QTableWidgetItem(str(data)))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
answered Nov 16 '18 at 6:27
rarevessellrarevessell
2118
2118
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53276062%2fhow-can-i-read-data-from-database-and-show-it-in-a-pyqt-table%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
This line in the
MainWindow.load()
method:result = session(Employee).query.all()
. The variableEmployee
is not defined anywhere in your module. Either you need to define theEmployee
model in you module, or import it from somewhere.– SuperShoot
Nov 13 '18 at 9:10
I will do that,thanks for the help
– rarevessell
Nov 13 '18 at 9:28
@ IIja Everila and SuperShoot,can you help with the code?
– rarevessell
Nov 13 '18 at 19:25