Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable to populate QTableView with PSQL database table.
Qt 6.12 LTS is out! See what's new in the release blog

Unable to populate QTableView with PSQL database table.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Spatula
    wrote on last edited by
    #1

    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_())
    
    M 1 Reply Last reply
    0
    • S Spatula

      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_())
      
      M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      Check the return value of your select() statement to see if it was successful.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Spatula
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • S Spatula

          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.

          M Offline
          M Offline
          mjsurette
          wrote on last edited by
          #4

          @Spatula
          FYI:
          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.

          Mike

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt
          • Unsolved