This works perfectly fine with an Oracle TABLE but not with a VIEW. Why?

uri = QgsDataSourceURI()
uri.setConnection(host, port, db, username, pw)
uri.setDataSource(schema, table, geomcol)
vlayer = QgsVectorLayer(uri.uri(), table, 'oracle')
print vlayer.isValid()

..and returns TRUE. However, the same code does not work with a VIEW. Both TABLE and VIEW have correct entry in USER_SDO_GEOM_METADATA. All spatial tables in the database load correctly; none of the views do. All spatial tables and views load correctly in QGIS using Layer->Add Layer->Add Oracle Spatial Layer. Is it meant to work only with tables or am I doing something wrong?

  • I think I meant QgsVectorLayer() in the title, not QgsDataSourceURI(). – Andy Oct 2 '15 at 12:59
  • ...and vlayer.isValid() returns FALSE with views; TRUE with tables. – Andy Oct 2 '15 at 13:05

For VIEWS you have to define the column which uniquely identify features for the layer, try to add:

  • I tried uri.setKeyColumn("your_unique_field") with a numeric and unique values column but unfortunately it does not work. These views have no primary key and Oracle does not require one either. I would like to know whether this is meant to work with Oracle views at all. Is there anybody who did this successfully? – Andy Oct 13 '15 at 12:55
  • Ok, it works but only if uri.setKeyColumn() follows uri.setDataSource(). Answer accepted! I now have another problem: stopping the 'coordinate reference system selector' popping up; uri.setSrid() and vlayer.setCrs() won't prevent it popping up... I'll try harder. Thanks for the uri.setKeyColumn() advice. – Andy Oct 13 '15 at 14:22

This works:

uri = QgsDataSourceURI()
uri.setConnection("doracle1.aytomalaga.intranet", "dbd3", "eqgis", "eqgis")

uri.setSrid ('25830');
uri.setDataSource('EQGIS','MSEQESLU_V', 'SDOGEOMETRIA', '', 'ID_EQESLU')


vlayer = QgsVectorLayer(uri.uri(),"capa", 'oracle')

You need to choose the version with five parameters of setDataSource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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