[gdal-dev] How to escape "desc" with OGR SQL?

Frank Broniewski frank.broniewski at gmail.com
Fri Jun 17 00:20:43 PDT 2016


Hi Jukka,

you will need to escape anything that can be interpreted as a control 
character on the command line. But it really depends on the 
interpretation context (console), but backslashes often need escaping as 
well like \n for a newline. This may even happen if you write something 
like \newspaper ... but for sql strings on the command line there's 
usually just the double quotes where you have to pay attention.
The problem you have here is that you are using sql keywords (desc and 
union) for your table aliases so you have to put double quotes around 
them. One way around this would be to not use sql keywords as table 
aliases at all. The next best solution would be to enclose your sql 
string into single quotes like Even already mentioned. Your double 
quotes don't "break" the sql string because they are different. But if 
you intend to use only double quotes for your sql string and your 
keywords you will need to escape the quotes around the keywords because 
otherwise they would break or signal an end to the sql string like it 
would on a normal sententence:
"I have " written "a sql string"
wouldn't be a correctly encapsulated sentence, would it? A backslash \" 
would remove the meaning of the " and not terminating the string.

HTH

Frank

Am 16.06.2016 um 19:59 schrieb Jukka Rahkonen:
> Even Rouault <even.rouault <at> spatialys.com> writes:
>
>   
>> On Windows shell I suppose ? Perhaps with \" (not tried)
>>
>> ogrinfo -sql "select attr_2 as \"desc\" from test" test.shp
> This really works for me on Windows.
>
>
>> On Linux bash the following - using single quote for the full SQL - also
> works
>> (not sure on Windows)
> For sure does not work on Windows.
>
>> ogrinfo -sql 'select attr_2 as "desc" from test' test.shp
>>
>>> ERROR 1: SQL Expression Parsing Error: syntax error, unexpected DESC,
>>> expecting identifier. Occurred around :
>>> select attr_2 as desc from test
>>>                   ^
>>> I managed to do the job with SQLite dialect but I wonder if I found a bug
>>> with using "desc" as an identifier.
>> Probably SQLite is more tolerant about using reserved keywords in non-
>> ambiguous situations.
> Ahh, I understand now. If I write just "desc" the double quotes are actually
> removed but SQLite does not care in this case.  But I can't do select... as
> "union" because now SQLite complains
> ERROR 1: In ExecuteSQL(): sqlite3_prepare(select attr_2 as union from test):
>    near "union": syntax error
>
> I must write it escaped select... as \"union\" instead so that double quotes
> are protected.
>
> Are there other characters than " which may need escaping? I guess % and _
> but probably there are more.
>
> -Jukka Rahkonen-
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
Waldhölzbacher Str. 51
66679 Losheim am See

(+49) 0177 30 21 791



More information about the gdal-dev mailing list