[Tutor] escape-quoting strings

Rick Pasotto rick at niof.net
Mon Nov 1 00:23:32 CET 2004


On Sun, Oct 31, 2004 at 03:06:56PM -0800, Marilyn Davis wrote:
> I got it!!
> 
> This works:
> 
> ad0 = address.replace('\\', '\\\\')
> ad0 = ad0.replace('\"','\\"')
> db_style0 = '''%%%s%%''' % ad0
> print "0:", db_style0
> 
> On Sun, 31 Oct 2004, Rick Pasotto wrote:
> 
> > On Sun, Oct 31, 2004 at 01:53:42PM -0800, Marilyn Davis wrote:
> > > Hi Python Tutors,
> > > 
> > > I'm having a devil of a time quoting strings properly for MySql.
> > 
> > You example did not include any MySql statements. How are you sending
> > the string to MySql?
> 
> I didn't want to complicate my question with MySql stuff.

But that part is important. Python's MySQLdb module takes care of and
simplifies the quoting. I suspect you're doing a lot of unnecessary
work.

> Here is a successful call that depended on the quoting scheme above:
> 
> insert ignore into doorman (in_id,  msg_id, out_address, status, start)
> values  (60, "1COOZD-0003KN-2z", "\"Toys \\\"R\\\" Us Gift Card Giveaway\"
> <fqgdmvfjfyvjd at dlawczhyh.montebic.com>", "NO_MESSAGE", "0")

You're *hard* *coding* the values? I thought you were working with a
string variable.

-- 
"And the fox said to the little prince: men have forgotten this truth,
 but you must not forget it. You become responsible, forever, for what
 you have tamed." -- Antoine de Saint-Exupery [The Little Prince]
    Rick Pasotto    rick at niof.net    http://www.niof.net


More information about the Tutor mailing list