title: How to properly escape strings when manually building SQL queries in SQLAlchemy?
body: <p>I am using SQLAlchemy to connect to different databases in Python, but not with the ORM support as this cannot be implemented due to several reasons.</p>
  
  <p>Mainly I do build a complex SQL query using things like</p>
  
  <pre><code>sql += "AND fieldname = '%s'" % myvar
  </code></pre>
  
  <p>In my case is not a problem of SQL injection as the data is always from a trusted source but even if the source is trusted it could contain characters that could break the query like <code>'</code>, <code>%</code> or <code>_</code>.</p>
  
  <p>Mainly, I need to escape them, and I wonder if there is an already existing escape function that I could re-use.</p>