擅长:python、mysql、java
<p>下面的解决方法对我有效:</p>
<pre><code>q = session.query(Genotypes).filter(Genotypes.rsid.in_(inall))
query_as_string = str(q.statement.compile(compile_kwargs={"literal_binds": True}))
session.execute(query_as_string).first()
</code></pre>
<p>这基本上迫使查询在执行之前编译为字符串,从而绕过了整个变量问题。SQLAlchemy的文档<a href="http://sqlalchemy.readthedocs.io/en/latest/faq/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined" rel="noreferrer">here</a>中提供了一些关于这个的详细信息。</p>
<p>顺便说一下,如果不使用SQLite,可以使用ANY运算符将list对象作为单个参数传递(请参阅我对这个问题的回答<a href="https://stackoverflow.com/questions/13190392/how-can-i-bind-a-list-to-a-parameter-in-a-custom-query-in-sqlalchemy/42591722#42591722">here</a>)。</p>