<p>我试图从列列表和值列表动态构建一个SQLWHERE子句。为此,我从以下代码开始:</p>
<pre><code>sql_cols = ['description', 'height', 'weight', 'class' ]
sql_vals = [ 1, 2, 3, 4 ]
sql_test = " and".join( " f = :{}".format(x) for x in sql_vals )
print sql_test
</code></pre>
<p>这给了我以下结果:</p>
<pre><code>root@scar$ python testme.py
f = :1 and f = :2 and f = :3 and f = :4
</code></pre>
<p>但我想得到的是这样的东西:</p>
<pre><code> `DESCRIPTION = :1 and HEIGHT = :2 and WEIGHT = :3 and CLASS = :4`
</code></pre>
<p>如何做到这一点?你知道吗</p>
<p>编辑:
将代码更改为:</p>
<p>sql\u test=“and”.join(sql\u cols[x-1]+”=:{}.format(x)for x in sql\u vals)</p>
<p>一切都成功了</p>