1

I have a query that searches an Oracle database using a where clause to get the geometry of an object and then zooms to the extent of that object. The value for the where clause is retrieved from a dialog that opens after a button press of a custom tool button.

I want to be able to test if the input in the where clause for the query matches what is in the database, and if not to produce an error message stating that the input was not recognised.

I have created a connection to Oracle and I can zoom to the extents of the object, but if the query does not match any results it zooms to the coordinates 0,0.

I have tried the following code and made several variations on it but the query always returns as valid, whether the input matches rows in the table or not.

uri = QgsDataSourceURI()
uri.setConnection("provider","port","database","user","pass")
uri.setDataSource("user","table_name","geometry_column","where_clause")
try:
    layer = QgsVectorLayer(uri.uri(),"layer_name","oracle")
        if layer.isValid():
            qgis.utils.iface.mapCanvas().setExtent(layer.extent())
            qgis.utils.iface.mapCanvas().refresh()
except:
    qgis.utils.iface.messageBar().pushMessage("Error","The input was not recognised.", level = QgsMessageBar.CRITICAL, duration = 15)

I tried the code from the accepted answer here but this gave the results of query.value(0) as None and the db parameters set were all empty, despite the values being explicity set. I don't know whether this is because it is Oracle and not PostgreSQL.

Is there a way to test that the database query matches rows from the table before acting upon the results?

  • Isn't there a method on the layer object to get the number of features it contains ? If that number is 0, then the query returned no rows. – Albert Godfrind Feb 19 '16 at 20:52
  • Thank you, I didn't think of that option as I got so stuck down one route. It worked checking for the feature count of the layer. – Nicola Feb 22 '16 at 9:18

Your Answer

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

Browse other questions tagged or ask your own question.