Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to extract all attributes from a non geomety layer in QGIS with Python, but there is only the function


More specific: I have a postgresql table which is loaded into QGIS with "Add PostGIS layers". Then I need all attributes from two columns in a dictionary (key: column1, value: column2). Here is my code so far:

dictAttr = {}
feature = QgsFeature()
while layer.dataProvider().nextFeature(feature):
  attributes = feature.attributeMap()
  dictAttr[attributes[FIELD_INDEX1].toString()] = attributes[FIELD_INDEX2].toString()
return dictAttr

Problem: layer is not a geometry table, so it has no features and the while loop is never entered. And I can't use uniqueValues() because I need every attribute. Do you know any way to solve this? This can't be so hard...

share|improve this question
up vote 2 down vote accepted

Call select passing False as third argument (i.e. fetchGeometry):

layer.dataProvider().select(layer.dataProvider().attributeIndexes(), QgsRectangle(), False)
share|improve this answer
so simple. I think I was confused with the spatial filter (empty QgsRectangle()) and didn't noticed the fetchGeometry attribute... – baal Dec 16 '12 at 16:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.