[Zope-DB] Cleaning apostrophes
Cynthia Kiser
cnk at caltech.edu
Wed Sep 15 19:06:18 EDT 2004
Why are you using / to escape the '? It appears to be legal but I
think the more normal escape for PostGres is to use two
apostrophies. From Practical PostGreSQL:
The fact that string constants are bound by single quotes presents an
obvious semantic problem, however, in that if the sequence itself
contains a single quote, the literal bounds of the constant are made
ambiguous. To escape (make literal) a single quote within the string,
you may type two adjacent single quotes. The parser will interpret
the two adjacent single quotes within the string constant as a single,
literal single quote. PostgreSQL will also allow single quotes to be
embedded by using a C-style backslash:
Quoting Lalo Castro <laloc at ucsc.edu>:
> I apologize if this is a simple enough question. I am something of
> a newbie...
> We are using Zope 2.6.0, python 2.1.3, and pSQl 7.3.4 on a freebsd4
> machine.
> There is a webform we have that has to be able to accept
> apostrophes. PostgreSQL sees the apostrophes as the end of string and
> errors out at the next character. We've been trying to build a python
> script to escape the apostrophes before they get sent off to the database,
> but the writing has become problematic. Here's the script:
>
> import string
> unapostrophated = string.replace(apostrophated, "'", "\'")
> return (unapostrophated)
>
> This doesn't work, as python sees the escape character as escaping
> the apostrophe and replaces, say, "here's" with "here's" (as opposed to
> "here\'s", which is what we want.
> We've also tried escaping the escape character ("\\'" which makes
> "here\\'s"), and raw text (r"\'" which makes "here\\'s"), to no
> solution. PostgreSQL sees the double backslash and errors out. Other
> attempts were ASCII text ("\034'") and the html code ("?034:'") and
> unicode for apostrophes ("\\a"). None of which worked.
> Is there a way to do this within the python script? If I am missing
> a solution using another method other than the python script, please let
> me know.
> Thanks in advance for any help.
> Lalo
> _______________________________________________
> Zope-DB mailing list
> Zope-DB at zope.org
> http://mail.zope.org/mailman/listinfo/zope-db
--
Cynthia Kiser
More information about the Zope-DB
mailing list