python - PyQt5/QSql:应用程序找不到 postgres 表
问题描述
通过我的业务应用程序学习 PyQt5。数据库后端是 postgres。有两个模块;一个定义 QSqlDatabase 并打开它,另一个应该在 QTableView 中显示一个小型查找表 activitytypes 的内容。日志显示找不到数据库表。我需要了解为什么以及如何在将来避免这个问题。
日志文件:
INFO:root:found database
DEBUG:root:Defining model/view
DEBUG:root:model error: Unable to find table activitytypes
DEBUG:root:about to execute select query
DEBUG:root:End of Program
activitytypes.py 模块:
import sys
import logging
# bug fix:
from pdb import Pdb; Pdb(skip=['importlib*']).set_trace()
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
from PyQt5 import QtSql as qts
from datasource import DBSetup
logging.basicConfig(level=logging.DEBUG, filename='activitytypes.log')
#logging.debug('running DBSetup')
#dbs.db
class ATWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
# Model/View here.
table_name = 'activitytypes'
act_tbl = DBSetup()
self.setWindowTitle(f'{act_tbl.db.databaseName()} - table_name}')
logging.debug('Defining model/view')
self.model = qts.QSqlTableModel()
self.model.setTable(table_name)
self.model.select()
model_error = self.model.lastError()
if model_error.isValid(): # i.e. there is an error
logging.debug(f"model error: {model_error.text()}")
else:
logging.debug("no model error")
# Query to collect data to display
self.query = qts.QSqlQuery()
breakpoint()
logging.debug(f'about to execute select query')
self.query.exec('select * from activitytypes')
self.table = qtw.QTableView()
breakpoint()
self.table.setModel(self.model)
self.setMinimumSize(qtc.QSize(800, 600))
self.setCentralWidget(self.table)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = ATWindow()
window.show()
#sys.exit(app.exec())
app.exec_()
logging.debug("End of Program")
datasource.py 模块:
import sys
import logging
# bug fix:
from pdb import Pdb; Pdb(skip=['importlib*']).set_trace()
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtSql as qts
class DBSetup:
""" Set up database connection """
def __init__(self, *args, **kwargs):
self.db = qts.QSqlDatabase.addDatabase('QPSQL')
self.db.setDatabaseName('bustrac')
logging.info('found database')
#self.db.setHostName('salmo')
#self.db.setUserName('rshepard')
#self.db.setPassword('ploofduh')
breakpoint()
if not self.db.open():
error = db.lastError().text()
qtw.QMessageBox.critical(
None, 'DB Connection Error',
f'Could not open database file: {error}')
sys.exit(1)
# Check that all database tables exist
required_tables = {'activities','activitytypes','industrytypes',
'locations','organizations','people','projects',
'statustypes'}
tables = self.db.tables()
missing_tables = required_tables - set(tables)
if missing_tables:
qtw.QMessageBox.critical(
None, 'DB Integrity Error',
f'Missing tables, please repair DB: {missing_tables}')
sys.exit(1)
解决方案
昨天得知Qt5在5.15.0版本中增加了对PostgreSQL-12的支持,安装的版本是5.12.3。解决方案是在我的 slackware-14.2 主机上构建 qt5-5.15.2。我现在正在做这件事。
推荐阅读
- http - 为什么我们要在授权标头中添加授权令牌?
- kubernetes - 除了 helm install/upgrade 的 --timeout 标志之外,有没有办法更改默认超时?
- python - Python 3.9 - 如何打印和格式化 .txt 文件?
- python - 安装应用程序 ios 创建 python 的机智 kivy 库。在IOS上安装
- c - 是什么导致头文件中的 C 中出现标识符错误?
- database - 带有颤振的 WEBRTC
- angular - Firestore 是否支持对中型项目的查询?
- javascript - 如何从函数外部访问和返回变量值
- r - 如何将函数的打印内容捕获到R中的变量中
- amazon-cloudformation - AWS CDK 资源创建顺序?