[Qgis-developer] pyqgis loading oracle table with sql expression

Médéric RIBREUX mederic.ribreux at medspx.homenet.org
Sat Jul 4 04:34:15 PDT 2015


Hi,

Oracle provider often needs more information to load a layer from an
SQL Query: 
* the wkbType (if it can't be deduced from the layer)
* the primary key column.

You could add the following lines before calling the layer:

uriOracle.setWkbType(QGis.WkbPolygon) # Or whatever WKBType
uriOracle.setKeyColumn(u"GID") # Use the name of the primary key of COMMUNE table

It should work better...

Furthermore, be sure that your query is valid (I am not sure that SORT BY
is a valid SQL clause, use ORDER BY).

If this is not the case, what does the Oracle tab of the log window
says ?

Best regards,

On Fri, Jul 03, 2015 at 04:27:58PM +0200, PIERRE Sylvain wrote:
> Hi,
> 
> I'm trying to load oracle table with sql expression like explain here:
> http://blog.georepublic.info/2013/joining-tables-with-sql-using-pyqgis/
> 
> My python code:
> uriOracle = QgsDataSourceURI()
> uriOracle.setSrid("EPSG:3948")
> uriOracle.setConnection('xxxxxxxx', 'xxxxx','xxxxx', 'consultsig','consultsig')
> 
> uriOracle.setDataSource( "", "(select * from COMMUNE sort by ccocom)", "GEOM", "","id")
> 
> layer = QgsVectorLayer(uriOracle.uri(), 'communes', 'oracle')
> if  not layer.isValid():
>   print "Layer failed to load!"
> 
> But layer is not valid.
> Did I miss something?
> (I want to load layer but already sorted on a field value...)
> Thanks
> 
> Sylvain
> 
> 
> 

> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer

--
Médéric RIBREUX
http://medspx.fr


More information about the Qgis-developer mailing list