title: How do I create a table in PyQt and populate it from a postgresql database?
body: <p>I'm new to python and looking for some help:</p>
  <p>I want to create a window and display a table in the window, populated from a postgresql table.</p>
  <pre><code>#!/usr/bin/python
  
  import psycopg2
  from psycopg2 import Error
  import sys
  from prettytable import PrettyTable
  from PyQt6.QtWidgets import QApplication, QWidget
  
  
  class Example(QWidget):
      def __init__(self):
          super().__init__()
  
          self.initUI()
  
  
      def initUI(self):
  
          try:
              # Connect to an existing database
              connection = psycopg2.connect(user=&quot;postgres&quot;,
                                          password=&quot;mydb&quot;,
                                          host=&quot;localhost&quot;,
                                          port=&quot;5432&quot;,
                                          database=&quot;mydb&quot;)
              
              # Create a cursor to perform database operations
              cursor = connection.cursor()
              
              # Executing a SQL query
              cursor.execute(&quot;SELECT * FROM table;&quot;)
  
              # Fetch result
              record = cursor.fetchall()
  
  
          except (Exception, Error) as error:
              print(&quot;Error while connecting to PostgreSQL&quot;, error)
          
          finally:
              if (connection):
                  cursor.close()
                  connection.close()
                  print(&quot;PostgreSQL connection is closed&quot;)
                  x=PrettyTable()
                  x.field_names = [&quot;workorder_id&quot;, &quot;part_id&quot;, &quot;workorder_total&quot;]
                  x.add_rows(record)
                  print(x)
  
          self.setGeometry(300, 300, 300, 200)
          self.setWindowTitle('Workorders')
          self.show()
  
  
  def main():
  
      app = QApplication(sys.argv)
      ex = Example()
      sys.exit(app.exec())
  
  
  if __name__ == &quot;__main__&quot;:
      main()
  
  </code></pre>
  <p>To clarify, I can use the psycog2 module to fetch information and print it to the console. However how do I go about actually creating a visual table, and populate the table from my <code>record</code> array</p>