MySQLdb and strings with quotes / long strings

Chris Keyes chrisk at
Fri Jan 24 20:09:02 CET 2003

Hi All

I'm trying to put email messages into a MySQL database. I'm happy enough 
with the 65k character Text field length limit, however I'm having 
trouble getting the data in.

I construct a SQL string insert into... set body='%s'... %(body)

body has had body = string.replace(body, "'", "''") done to it, which 
should escape the strings with quotes in, but I get this traceback

Traceback (most recent call last):
   File "", line 85, in ?
     sql.add_message(db, message.get("From"), 
email.Utils.parsedate(message.get("Date")), message.get("SUBJECT"), body)
   File "", line 74, in add_message
   File "/usr/lib/python2.1/site-packages/MySQLdb/", line 61, 
in execute
     r = self._query(query)
   File "/usr/lib/python2.1/site-packages/MySQLdb/", line 168, 
in _query
     rowcount = self._BaseCursor__do_query(q)
   File "/usr/lib/python2.1/site-packages/MySQLdb/", line 112, 
in __do_query
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your 
SQL syntax near 't actually do it''\n    print ''\\t-p\\tmanager 
permissions, in the form''\n    prin' at line 1")

looks like the escaping did not work? How have other people got round 
this one?

All ideas/advice appreciated.



More information about the Python-list mailing list