0

I'm trying use the QSqlDatabase in my code.

The connection work well when I use the port 5432 to connect on my PG database. But I can't see any result when try use another port, my tableview is empty, but the connection work. below my code:

from PyQt5.QtSql import QSqlDatabase, QSqlTableModel

db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.databaseName("my_db")
db.setPort(5432)
db.setUserName("postgres")
db.setPassword("postgres") 
            
db.open()
self.model_a = QSqlTableModel(db = db) 
           
self.table.setModel(self.model_a) 
self.model_a.setTable("us_table")
self.model_a.select()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
enzo cocca
  • 35
  • 1
  • 7
  • How are you sure that QSqlDatabase was connected to the database? What does `print(db.open())` return? – eyllanesc Aug 26 '20 at 19:19
  • @eyllansec yes I'm sure. I tryed on two different postgres. one on port 5432 and works and one on port 5433 but dosn't work. if print db.open() both are connected but the first show me the result and the other no – enzo cocca Aug 27 '20 at 05:14
  • when use the port 5432 the model_a.select() return true else when use different port the model_a.select() return false. someone knows why? – enzo cocca Aug 27 '20 at 05:28
  • Use `if not self.model_a.select():` `print(self.model_a.lastError().text())` to see if there are any errors: – eyllanesc Aug 27 '20 at 05:32
  • it can't find the table us_table. it's strange because the table us_table exist and they are two identical database on two different postgres – enzo cocca Aug 27 '20 at 05:46
  • what is the output of `print(db.tables())` after `db.open()`? – eyllanesc Aug 27 '20 at 05:51
  • it print all tables – enzo cocca Aug 27 '20 at 05:58
  • if try with a different table I have the same result: it can't find the table "any table" – enzo cocca Aug 27 '20 at 06:05
  • I understand that if you have two postgres on the same machine and on the same host, QSqlTableModel list just one server on the port with the less value. Is it true this? – enzo cocca Aug 27 '20 at 14:53

0 Answers0