Strings: double versus single quotes

Roel Schroeven roel at roelschroeven.net
Sat May 23 14:52:02 EDT 2020


Skip Montanaro schreef op 23/05/2020 om 13:03:
> I also agree about SQL. I found that something like this:
> 
> stmt = (
>      """select foo from bar"""
>      """  where a = 'bag'"""
>      """    and c = 'dog'"""
> )
> 
> worked pretty well, served to both satisfy my brain's desire for semantic
> indentation (you should see some of the SQL I inherited - yikes!) and
> maintain a visual distinction between Python and SQL quoting. (Consistently
> using triple quotes minimizes the chance of needing a stray Python
> backslash inside the SQL code.)

May I ask why not simply like this:

stmt = """
      select foo from bar
        where a = 'bag'
          and c = 'dog'
      """

This introduces more newlines and spaces in the query, but that
shouldn't really matter. I think it's more readable, and easier to edit
the query.

> I'm now retired, so can't double check, but
> I believe SQLite and MSSQL are unusual in their Pythonesque treatment of
> single and double quotes being synonymous. I believe most other dialects
> (Oracle, MySQL, Sybase, PostgreSQL that I checked) only recognize single
> quotes as string delimiters.

Right. The SQLite developers did it that way in an effort to be
compatible with MySQL, and since have come to the realisation that that
was a mistake. In recent versions you can turn it off, in which cases
single quotes are for string literals and double quotes are for
identifiers (such as column names), as in standard SQL.
See
https://sqlite.org/quirks.html#double_quoted_string_literals_are_accepted

-- 
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
          -- Franklin P. Jones

Roel Schroeven



-- 
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
         -- Franklin P. Jones

Roel Schroeven



More information about the Python-list mailing list