sqlsafetext

michael montagne montagne at boora.com
Tue May 8 11:52:21 EDT 2001


Thank you all for your help.
I used the line:
strText = strText.replace("'", "''").replace('"', '""')
and it worked beautifully.  In the future, I will endeavor to provide more
information in my questions.

-mjm

"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9d77lm07fq at news1.newsguy.com...
> "michael montagne" <montagne at boora.com> wrote in message
> news:FoEJ6.43651$FS3.483452 at sjc-read.news.verio.net...
> > Below is a little function I use to handle such things as "o'brien" in
SQL
> > strings.  It works in VB and I'm trying to port the function to Python.
> If
> > I type it in interactively, replace returns what I would expect.
> > (o''brien), But the function doesn't work.  Why?
>
> Because you're not assigning the result of your string.replace
> calls to anything.  Remember, string objects are immutable:
> functions such as string.replace return OTHER string objects,
> they DON'T mutate the ones they were given as arguments
> (because they can't!).  So, you need some rebiding via
> assignment statements:
>
> >
> > def sqlsafetext(strText):
> >       if strText=="":
> >          return "Null"
> >       else:
> >          #print strText
> >          import string
> >          #replace single quotes with double single quotes
> >          string.replace(strText,chr(39),chr(39) + chr(39))
>
>         strText = strText.replace("'", "''")
>
> &tc.
>
>
> Alex
>
>
>





More information about the Python-list mailing list