title: Escaping single and double quotes for mysql in python
body: <p>I am using mysql.connector python library with python 2.7</p>
  
  <p>I have a unicode string which may or may not contain single and double quotes.
  Here are the things I tried for my escape function:</p>
  
  <pre><code>def escape(string):
  #string.MySQL.escape_string()
  #string = string.decode('string_escape')
  #string = string.encode('unicode-escape').replace("'", "''")
  #string = string.encode('unicode-escape').replace('"', '\"')
  #string = string.encode('unicode-escape').replace("'", u"\u2019")
  #string = string.encode('unicode-escape').replace('''"''', u"\u201D")
  #string = string.encode('unicode-escape').replace('''''', u"\u201D")
  return string
  </code></pre>
  
  <p>Nothing seems to have worked. I tried using <a href="https://dev.mysql.com/doc/connector-python/en/connector-python-api-cext-escape-string.html" rel="nofollow">this</a> function but still gives mysql syntax error.</p>
  
  <p>I need something within mysql.connector library which escapes the single and double quotes without breaking the unicode as well as mysql query. </p>
  
  <p>Here is an example of SQL query I am using:</p>
  
  <pre><code>"""SELECT * FROM messages WHERE msg_id = '{msg_id}'""".format(**db_dict)
  </code></pre>
  
  <p>Let me know if any more details needed</p>
  
  <p><strong>EDIT</strong>: Example SQL query updated</p>