sqlsafetext
Fredrik Lundh
fredrik at pythonware.com
Mon May 7 18:07:18 EDT 2001
michael montagne wrote:
> 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?
next time you post a question, maybe you could define
"doesn't work" a bit more carefully (like, say, including any
exceptions you got, etc).
> 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))
saving the result of the "replace" call in a variable might
help:
strText = string.replace(strText,chr(39),chr(39) + chr(39))
or, using string methods instead of functions:
strText = strText.replace("'", "''")
strText = strText.replace('"', '""')
> #replace double quotes with double double quotes
> string.replace(strText,chr(34),chr(34)+ chr(34))
> #wrap in single quotes
> strtemp="'" + strtemp + "'"
where did "strtemp" come from? are you sure you didn't get
a NameError exception on that line?
> return strText
Cheers /F
More information about the Python-list
mailing list