title: How do I get a raw, compiled SQL query from a SQLAlchemy expression?
body: <p>I have a SQLAlchemy query object and want to get the text of the compiled SQL statement, with all its parameters bound (e.g. no <code>%s</code> or other variables waiting to be bound by the statement compiler or MySQLdb dialect engine, etc).</p>
  
  <p>Calling <code>str()</code> on the query reveals something like this:</p>
  
  <pre><code>SELECT id WHERE date_added &lt;= %s AND date_added &gt;= %s ORDER BY count DESC
  </code></pre>
  
  <p>I've tried looking in query._params but it's an empty dict.  I wrote my own compiler using <a href="http://groups.google.com/group/sqlalchemy/browse_thread/thread/0c20a4f380e277af" rel="noreferrer">this example of the <code>sqlalchemy.ext.compiler.compiles</code> decorator</a> but even the statement there still has <code>%s</code> where I want data.</p>
  
  <p>I can't quite figure out when my parameters get mixed in to create the query; when examining the query object they're always an empty dictionary (though the query executes fine and the engine prints it out when you turn echo logging on).</p>
  
  <p>I'm starting to get the message that SQLAlchemy doesn't want me to know the underlying query, as it breaks the general nature of the expression API's interface all the different DB-APIs.  I don't mind if the query gets executed before I found out what it was; I just want to know!</p>