19

I am trying different queries in the Query Builder. Is there a way to comment the query? I tried with

/* */

#

--

without success :

An error occurred when executing the query. The data provider said: OGR[3] error 1: SQL Expression Parsing Error: syntax error

EDIT :

  • I have made a feature request of this question: http://hub.qgis.org/issues/10914

  • @Bernd V. observed that expressions are evaluated differently according to the format of the vector (see the [help] button in the Query builder for more information)

nebi
  • 611
  • 5
  • 16
  • Welcome to GIS! Unless I'm mistaken, I don't think you can comment queries in the Query Builder. – Joseph Jul 17 '14 at 12:18
  • Is there an other way to build queries then (and comment them as required ?) (Thx for editing, it looks much better) – nebi Jul 17 '14 at 12:48

2 Answers2

16

It doesn't work with ESRI shapefile. However, commenting in expressions works in QGIS 2.14 with SpatiaLite, SQlite and GeoPackage. For most other formats available in the Save as...-dialog, the query fails with an OGR-error.

Use -- for one-line comments and /* ... */ for multi-line comments

It turns the letters to a green color in the expression builder window and they seem indeed to be commented out!

Bernd V.
  • 3,179
  • 25
  • 49
  • Awesome! I don't have QGIS 2.8.2 to test but it is mentioned here so I believe you :) In which case, your answer should be accepted! – Joseph Jun 02 '15 at 12:18
  • and @Joseph: are you sure you're in the Query Builder? I can't reproduce it. It turns the letters to green but it still doesn't work (2.8.2-Wien). – nebi Jun 04 '15 at 08:11
  • @nebi - I also cannot reproduce it in the Query Builder (also installed 2.8.2 Wien). Only seems to work in the Field Calculator. Have you opened a ticket (feature request) for this? – Joseph Jun 04 '15 at 11:32
  • didn't test it beforehand in the Query Builder (sorry) but with rule based rendering expressions etc, cause I hardly ever use that thing. But here it works as well. Am on Linux Mint, maybe there is a difference/bug in Windows? – Bernd V. Jun 04 '15 at 12:22
  • Ha! Seems I found the difference. I was using it successfully with a Spatialite-Layer. Now I tested it with a ESRI-Shape ... and it fails! – Bernd V. Jun 04 '15 at 12:36
  • Awesome. Could @nebi change the accepted answer so it is easier to find? – Toivo Säwén Feb 06 '17 at 14:01
  • @ToivoSäwén : updated question and answer (pending) to make it more understable – nebi Feb 07 '17 at 14:57
  • There is a bug in QGIS 3.14. Inline comments return incorrect results: https://github.com/qgis/QGIS/issues/38359 – Comrade Che Aug 19 '20 at 09:36
6

You can build queries by using the Query Builder or an alternative is to use the Expression string builder (Layer Properties > Style > select Rule-based > add a rule > insert your command in the Filter box or click the '...' button to bring up the string builder interface.

In both cases, I do not think QGIS supports the comments function when creating expressions for filters/queries:

http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/expressions.html

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • True, with this way you can save your rules and import / export them ... But it's not as quick as commenting with some symbol at the beginning of the line. Copy / paste in the query Builder is tedious – nebi Jul 17 '14 at 13:58
  • 3
    @nebi, you could always send a request for this to be implemented in future updates. – Joseph Jul 17 '14 at 14:11
  • Yes, I want to do it – nebi Jul 17 '14 at 14:12