What is the right way to use PostgreSQL with PyQt5

Nenad Lamza nenad.lamza at sk.t-com.hr
Fri Jun 12 17:32:50 BST 2020


Thanks Steve and Dennis.

You really made an effort to explain the concept and I think I get it.

Steve, I run your code and it works ok, and also analysed the code. 
ODTableModel is simpler version as a proof of concept. ObjectTableModel 
is what one should use in the app. With it, the source and nature of the 
object can by whatever, sqlalchemy objects or something else. And I 
suppose that concept works great.

Dennis, you further explained the concept, thank you.

 >So there is no need for any PyQT direct interaction with the database
 >and in fact that would be over-complicating the program further it
 >appears there would be no driver issues either and you are NOT inventing
 >a new MVC (or M-C-V) you are just using the concept and completely
 >divorcing your front-end from your back-end to keep them autonomous
 >which helps in many many ways to make your code much more solid-state as
 >well as helps with maintainability and efficiency. Remember K.I.S.S. It
 >is better than M.I.C.C. It --- (Keep It Simple and Smart) vs (Make It
 >Complex and Crappy pronounced Miss)

So far, I only used standard (Py)Qt database Sql classes:

query = QSqlQuery()
queryString = ("SELECT ...{}...{}...").format(...)
query.exec(queryString)
#record = {}
if query.next():
      record = query.record()
      field1 = record.value("field1")
      field2 = record.value("field2")
      ...
      #or:
      #record["field1"] = record.value("field1")
      #record["field2"] = record.value("field2")
      #...

That way I don't have real business objects and ORM, just variables (or 
dict) and manipulating with it is probably harder. I thought that was 
enough for my apps because I get all those Sql classes out of the box 
and they work ok. I use QSqlQueryModel with Delegates, and don't use 
QSqlTableModel (a toy).

Your approach is probably better and more flexible. I'll sure try it 
when I have time.

But my question was really about PostgreSQL drivers. Is there any 
solution for that in PyQt.

Maybe Phil could suggest something.



More information about the PyQt mailing list