前几天又有同事掉进了给 SQL 的 IN 条件传参的坑,就像 SELECT col1, col2 FROM table1 WHERE id IN (1, 2, 3) 这类 SQL,如果是一个可变的列表作为 IN 的参数,那这个参数应该怎么传呢?more我见过至少这么几种:12id_list=[1,2,3]cursor.execute('SELECT col1, col2 FROM table1 WHERE id IN (%s)',id_list)这种方式是语法错误的,原因是 MySQLdb 做字符串格式化时占位符和参数个数不匹配。12id_list=[1,2,3]cursor.execute('SELECT col1, col2 FROM table1 WHERE id IN (%s)',(id_list,))这种方式语法是正确的,但语义是错误的,因为生成的 SQL 是 SELECT col1, col2 FROM table1 WHERE id IN ((‘1’, ‘2’, ‘3’))123id_list=[1,2,3]id_list=','.join([str(i)foriinid_list])cursor.execute('SELECT col1, col2 FROM table1 WHERE id IN
...
继续阅读
(44)