Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://docs.sqlalchemy.org/en/latest/core/tutorial.html#using-text" rel="noreferrer">The tutorial</a> gives a pretty good example for this:</p> <pre><code>&gt;&gt;&gt; from sqlalchemy.sql import text &gt;&gt;&gt; s = text( ... "SELECT users.fullname || ', ' || addresses.email_address AS title " ... "FROM users, addresses " ... "WHERE users.id = addresses.user_id " ... "AND users.name BETWEEN :x AND :y " ... "AND (addresses.email_address LIKE :e1 " ... "OR addresses.email_address LIKE :e2)") SQL&gt;&gt;&gt; conn.execute(s, x='m', y='z', e1='%@aol.com', e2='%@msn.com').fetchall() [(u'Wendy Williams, wendy@aol.com',)] </code></pre> <p>First, take your SQL string and pass it to <a href="http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.text" rel="noreferrer">sqalchemy.sql.text()</a>. This isn't necessary, but probably a good idea...</p> <blockquote> <p>The advantages text() provides over a plain string are backend-neutral support for bind parameters, per-statement execution options, as well as bind parameter and result-column typing behavior, allowing SQLAlchemy type constructs to play a role when executing a statement that is specified literally.</p> </blockquote> <p>Note that even if you didn't use <code>text()</code>, you should NEVER just use <code>sql.format(...)</code>. This leads to greater risk of <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="noreferrer">SQL injection</a> attacks.</p> <p>Next, you can specify the actual arguments using keyword parameters to the <a href="http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Connection.execute" rel="noreferrer">execute()</a> function you've already been using.</p> <p>Now, in your example, you have a function that wraps the execute functionality. So, if you want to use this for multiple queries, you'll need to make the parameters able to receive your arguments. You could do this pretty simple as a dictionary:</p> <pre><code>def _sql_to_data(sql, values): ... conn.execute(sql, values) </code></pre> <p><code>values</code> would be a dictionary.You could then use your function like this...</p> <pre><code>sql = 'SELECT ...' data = { 'user_id' : 3 } results = _sql_to_data(sql, data) </code></pre> <p>Using keywords as your parameters is just one way of specifying the arguments to the <code>execute()</code> function. You can read <a href="http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Connection.execute" rel="noreferrer">the documentation</a> for that function for a few different ways.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload