1

I'm new to PyQt programming and I was asked to build a GUI for a Postgres database. I'm trying to use the QtSql, but when I try to return a value after executing a query, I don't get the value I'm looking for.

db = QSqlDatabase.addDatabase("QPSQL", "aib")
db.setHostName("my address")
db.setDatabaseName("aib")
db.setUserName("mayonnaise")
db.setPassword("ketchup")
ok = db.open()
if ok:
    print "open"
query = QtSql.QSqlQuery(db)
sql= "SELECT provincia FROM incendi.incendio where incendio.\"codInc\"='19810101230801'"
q=query.exec_(sql)
print query.numRowsAffected()
j=query.first()
if j:
    print "first record"
if query.isValid():
    print "valid"
prov=query.value(0)
print str(prov)

When I try to run it this is the output I get:

open
1
first record
valid
<PyQt4.QtCore.QVariant object at 0x00000000030F5388>

The SQL query works fine in pg-admin and returns the expected value, so is anything wrong in the python code?

Luca
  • 11
  • 2
  • You should use PyQt5 and Python3 if you are developing a new application. PyQt4 is obsolete legacy software that is no longer supported, and very soon support for Python2 will aslo end. – ekhumoro Dec 05 '18 at 12:18
  • @ekhumoro I know, but PyQt4 and Python 2.7 is what my committant asked and since I'm working for a state agency in Italy i have little to no freedom of action. I've already told them that they should move to Python 3 but they said it's impossible right now, so I'm stuck with this. – Luca Dec 06 '18 at 10:20
  • In that case, see [this answer](https://stackoverflow.com/a/40916776/984421) or [this answer](https://stackoverflow.com/a/44746227/984421). – ekhumoro Dec 06 '18 at 17:28
  • Thanks! I've actually solved it by using the psycopg2 module rather than the QtSql. – Luca Dec 10 '18 at 10:03

0 Answers0