from MySQLdb.converters import get_codec

def prepare_sql(cursor, query, args=None):
    if args is not None:
        query = query % tuple(get_codec(a, cursor.encoders)(db, a) for a in
            args)
return query
How to display an SQL string that has been constructed by MySQLDB cursor?