【发布时间】:2017-03-03 22:08:55
【问题描述】:
我的要求是检查表中是否有与列的值列表匹配的条目。我正在使用以下查询:
insert_enquiry = """SELECT P_ID,Jobname FROM build_table WHERE Build_number IN %s;""" % insert_constraint
insert_constraint 这里是一个数字列表。 Build_number 是一个包含整数的列。如果我尝试使用以下方式执行此查询:
cursor.execute(insert_enquiry)
我收到以下错误:
MySQL Error [1064]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[184, 931, 1005, 1070, 165, 99, 365, 930, 164, 98' at line 1
我相信这是因为列表中出现了方括号。我试图通过将列表转换为字符串来删除它们,但是我得到了与现在整数列表转换为字符串相同的错误。
我也尝试过以下语法:
insert_enquiry = """SELECT P_ID,Jobname FROM product_build WHERE Build_number IN %s;"""
cursor.executemany(insert_enquiry, insert_constraint)
这给出以下错误:
MySQL Error [1064]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '184' at line 1
编辑:
insert_enquiry = """SELECT P_ID,Jobname FROM product_build WHERE Build_number IN (%s);"""
cursor.executemany(insert_enquiry, insert_constraint)
由于某种原因,这仅采用列表 insert_constraint 中的最后一个元素。
【问题讨论】:
-
难道不是 IN(...) 而不是 IN[...] 吗?
-
是的,我已对问题进行了编辑。它以某种方式获取列表中的最后一个元素 insert_constraint 并跳过列表中的所有元素。
标签: python mysql mysql-python