def _escape_string(self, s):
        #TODO how fast is this?, do this in C/pyrex?
        escaped = []
        for ch in s:
            if ch == '\0':
                escaped.append('\\0')
            elif ch == '\n':
                escaped.append('\\n')
            elif ch == '\r':
                escaped.append('\\r')
            elif ch == '\\':
                escaped.append('\\\\')
            elif ch == "'": #single quote
                escaped.append("\\'")
            elif ch == '"': #double quote
                escaped.append('\\"')
            elif ch == '\x1a': #EOF on windows
                escaped.append('\\Z')
            else:
                escaped.append(ch)
        return ''.join(escaped)
take from mysql src code:
dataset/ETHPy150Open concurrence/concurrence/lib/concurrence/database/mysql/dbapi.py/Cursor._escape_string