在python中,编写带in的语句实现动态传入值的实现方法。将列表转换为元组再传入字符串的sql中

例如在mysql中的语句为:

select * from view_check_cmd c where c.cmd_id='1' and c.ip in ('192.168.1.70', '192.168.1.61', '192.168.1.62')


而在python中写为:

'''select * from view_check_cmd c where c.cmd_id='1' and c.ip in {} '''.format(tuple(ip_list))



原文链接:https://blog.csdn.net/tainyu/article/details/123467175

注意:

当变量变量只有一个时,使用.format(tuple(ip_list)) 会报错。

我把in 换成了 =

'''select * from view_check_cmd c where c.cmd_id='1' and c.ip = {} '''.format(ip_list[0])

注意判断变量是一个值,还是多个值。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐