[PyQt] QSqlTableModel and PostgreSQL 12
Sibylle Koczian
nulla.epistola at web.de
Wed Oct 9 13:37:14 BST 2019
Hello,
After upgrading from PostgreSQL 11.5 to version 12 I can't use
QSqlTableModel any more, and I don't understand what happens.
First, versions of everything:
PostgreSQL 12 on Windows 10;
PyQt 5.13.1 on ArchLinux (in a virtual machine);
Python 3.7.4.
Using a QSqlQuery with one of my PostgreSQL databases works as expected.
Trying to use a QSqlTableModel (this should run if the names replaced by
*** are adapted):
import logging
import sys
from PyQt5 import QtCore
from PyQt5 import QtSql
def fbConn():
db = QtSql.QSqlDatabase.addDatabase("QPSQL")
db.setDatabaseName("***")
db.setHostName("***")
db.setUserName("***")
db.setPassword("***")
ok = db.open()
msg = "No error" if ok else f"Database Error: {db.lastError().text()}"
return (ok, msg)
def create_stamm_model():
model = QtSql.QSqlTableModel()
logging.debug("Message by model: %s", model.lastError().text())
db = model.database()
logging.debug("Data base tables: %s", db.tables()) # <-- correct
answer
model.setTable("***") # <-- name is contained in the list from
db.tables()
logging.debug("Message by model.setTable: %s",
model.lastError().text()) # <-- error, see below
ok = model.select()
logging.debug("Message by model.select: %s, successful: %s",
model.lastError().text(), ok)
logging.debug("Model definition: %s", model.selectStatement())
return model
def main(args):
logging.basicConfig(level=logging.DEBUG)
app = QtCore.QCoreApplication(args)
(ok, msg) = fbConn()
if ok:
logging.debug("Message by fbConn(): %s", msg)
else:
logging.error(msg)
sys.exit(1)
create_stamm_model()
print("Auf Wiedersehen")
if __name__ == "__main__":
main(sys.argv)
Result of executing this script:
[sib at krise-arch fussball_tests]$ ./fb_qt_mail.py
DEBUG:root:Message by fbConn(): No error
DEBUG:root:Message by model:
DEBUG:root:Data base tables: ['t_manip', 't_spiel', 't_stamm',
't_ewtab', 't_pps', 't_tab']
DEBUG:root:Message by model.setTable: Unable to find table t_ewtab
DEBUG:root:Message by model.select: Unable to find table t_ewtab,
successful: False
DEBUG:root:Model definition:
Auf Wiedersehen
Is this a problem with PyQt5 or with Qt?
Thank you for help,
Sibylle
More information about the PyQt
mailing list