Escaping Strings

Lawrence D'Oliveiro <[email protected]> Mon, 15 Jul 2024 22:46:15 -0000 (UTC)
Newsgroups comp.databases.mysql
Organization A noiseless patient Spider
Message-ID <[email protected]>
In standard SQL, backslashes have no special significance. String literals 
are delimited by single quotes, and any characters (except single quotes) 
are allowed in them; to include a single quote in the string, write it 
twice.

So in Python, this expression is sufficient to turn a string ā€œsā€ into a 
standard SQL string literal:

    "'" + s.replace("'", "''") + "'"