Ich habe es herausgefunden, indem ich in /usr/lib/pymodules/python2.6/MySQLdb/connections.py nachsah, wie connection.escape aufgerufen wird. Etwas Herumschnüffeln führt zu MySQLdb.converters.conversions. Ein Auszug:
{0: <class 'decimal.Decimal'>, 1: <type 'int'>, ... <type 'dict'>: <built-in function escape_dict>, <type 'NoneType'>: <function None2NULL>, <type 'str'>: <function Thing2Literal>, <type 'tuple'>: <built-in function escape_sequence>, <type 'unicode'>: <function Unicode2Str>, <type 'bool'>: <function Bool2Str>} Verwendung:
import MySQLdb import MySQLdb.converters import datetime now=datetime.datetime.now() connection=MySQLdb.connect( host=HOST,user=USER,passwd=PASS,db=MYDB) print(connection.escape((1,2,now),MySQLdb.converters.conversions)) # ('1', '2', "'2010-07-24 19:33:59'") Nachtrag zu Bobby Tables: Im Normalfall müssen Sie Argumente nicht manuell escapen. Nutzen Sie parametrisierte Argumente in cursor.execute, dann quotet MySQLdb sie automatisch:
sql='insert into students (name,grade,date) values (%s, %s, %s)' args=("Robert'); DROP TABLE Students; --",60,now) cursor=connection.cursor() cursor.execute(sql,args)