I'm writing a python script using QGIS API. I have to get results from postgres database table. The table has geometry field and other non-geometry fields.
I want to perform SQL queries on non-geometry fields and get results in my code. How to do this? Is there any class to do so?
I know about QgsDataSourceURI
class but as far as I know it only gets results from geometry fields.
Best Answer
1) With PyQt4.QtSql: Gary Sherman shows how to query a spatial table in Creating a PostgreSQL Connection From a QGIS Layer Datasource:
And you can open all the other tables/views (spatial or non spatial) similarly:
2) Or you can use the standard Python module for PostgreSQL/PostGIS: Psycopg2:
With a non spatial table or view and the result as a dictionary: