Unable to populate QTableView with PSQL database table.
I have just started using PyQt and am trying to test certain things before i start making an application, but i cant get my QTableView to show a Table from my PSQL database. I managed to show a string list in the table view, but when i try with a QSqlTableModel i get a blank screen. This is my code, any help would be greatly appreciated
from PyQt5.QtSql import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * import sys from PyQt5.uic import * app = QApplication(sys.argv) class ProbaTabele(QWidget): def __init__(self,parent = None): super(ProbaTabele,self).__init__(parent) db = QSqlDatabase.addDatabase("QPSQL") db.setHostName("localhost") db.setPort(5432) db.setDatabaseName("Otest") db.setUserName("postgres") db.setPassword("bizmark0") if (db.open() == False): QMessageBox.critical(None, "Database Error", db.lastError().text()) self.db = db self.model = QSqlTableModel(self, self.db) self.model.setTable("Tabela1") self.model.setEditStrategy(QSqlTableModel.OnManualSubmit) self.model.select() self.view = QTableView(self) self.view.setModel(self.model) if __name__ == '__main__': view = ProbaTabele() view.show() sys.exit(app.exec_())
Check the return value of your select() statement to see if it was successful.
I had an upper case T in my table name, turns out that was the problem(A bug with PSQL databases and PyQt i guess?), i changed my table name to all lower case and my table view works now.
I use PSQL + C++ and tried inserting an uppercase letter in one of my table names with no adverse effects. I would expect PyQt to just pass the table name you give it. Perhaps it's Postgresql version dependent. I'm using 9.5.