sqlsafetext
Bjorn Pettersen
BPettersen at NAREX.com
Mon May 7 17:47:36 EDT 2001
> From: michael montagne [mailto:montagne at boora.com]
>
> 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?
>
> 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))
> #replace double quotes with double double quotes
> string.replace(strText,chr(34),chr(34)+ chr(34))
> #wrap in single quotes
> strtemp="'" + strtemp + "'"
> return strText
You're not saving the temporaries, ie. you probably meant to say strText =
string.replace(...).
Also, it's probably clearer if you use the characters themselves instead of
their chr equivalents:
strText = strText.replace("'", "''").replace('"', '""')
-- bjorn
More information about the Python-list
mailing list