我试图从列列表和值列表动态构建一个SQLWHERE子句。为此,我从以下代码开始:
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
这给了我以下结果:
root@scar$ python testme.py
f = :1 and f = :2 and f = :3 and f = :4
但我想得到的是这样的东西:
`DESCRIPTION = :1 and HEIGHT = :2 and WEIGHT = :3 and CLASS = :4`
如何做到这一点?你知道吗
编辑: 将代码更改为:
sql\u test=“and”.join(sql\u cols[x-1]+”=:{}.format(x)for x in sql\u vals)
一切都成功了
您可以使用此选项:
试试这个:
试试这个:
相关问题 更多 >
编程相关推荐