python sql语句中参数化方式传入in值方法

2022-12-29 08:46:37

在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])

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

  • 作者:做测试的喵酱
  • 原文链接:https://miaojiang.blog.csdn.net/article/details/125284370
    更新时间:2022-12-29 08:46:37