sqlite3 double quote behavior

Roel Schroeven roel at roelschroeven.net
Tue Dec 13 03:51:07 EST 2022


Op 13/12/2022 om 1:41 schreef John K. Parejko:
> Asking here before I file an improvement request issue on the python GitHub:
>
> sqlite has a known misfeature with double-quoted strings, whereby they will be interpreted as string literals if they don’t match a valid identifier [1]. The note in the sqlite docs describe a way to disable this misfeature at compile time or by calling an `sqlite3_db_config` C-function, but I don’t see any way to do that in the python sqlite library [2].
>
> Am I missing a way to manage this setting, or is it not available within python? This would be very useful to enable, so that python’s sqlite library will treat queries more like standard sql, instead of this particular version of MySQL. I was just burned by this, where some tests I’d written against an sqlite database did not fail in the way that they “should” have, because of this double-quoted string issue.
>
> It doesn’t look like `sqlite3_db_config` is used within the python sqlite3 codebase at all, so this might not be a trivial change? I only see two references to it in the cpython github.
>
Like Lars Liedtke this is not an exact answer to your question, but you 
can side-step the issue by using parametrized queries, i.e. instead of

     cur.execute('SELECT name, location FROM persons WHERE name = "John 
Doe"')

do

     cur.execute('SELECT name, location FROM persons WHERE name = ?', 
('John Doe',))


-- 
"Life ain't no fairy tale
Just give me another ale
And I'll drink to Rock 'n Roll"
         -- Barkeep (The Scabs)



More information about the Python-list mailing list