MySQLdb and strings with quotes / long strings

Chris Keyes chrisk at nipltd.com
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 "mailboxreader.py", line 85, in ?
     sql.add_message(db, message.get("From"), 
email.Utils.parsedate(message.get("Date")), message.get("SUBJECT"), body)
   File "sql.py", line 74, in add_message
     curs.execute(sqlstr)
   File "/usr/lib/python2.1/site-packages/MySQLdb/cursors.py", line 61, 
in execute
     r = self._query(query)
   File "/usr/lib/python2.1/site-packages/MySQLdb/cursors.py", line 168, 
in _query
     rowcount = self._BaseCursor__do_query(q)
   File "/usr/lib/python2.1/site-packages/MySQLdb/cursors.py", line 112, 
in __do_query
     db.query(q)
_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.

Thanks

Chris





More information about the Python-list mailing list