Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MySQLdb python中检查数据库中是否存在多个值?_Python_Mysql_Mysql Python - Fatal编程技术网

在MySQLdb python中检查数据库中是否存在多个值?

在MySQLdb python中检查数据库中是否存在多个值?,python,mysql,mysql-python,Python,Mysql,Mysql Python,我的要求是检查表中是否有与列的值列表匹配的条目。我正在使用以下查询: insert_enquiry = """SELECT P_ID,Jobname FROM build_table WHERE Build_number IN %s;""" % insert_constraint 这里是一个数字列表。Build_number是一个包含整数的列。如果我尝试使用以下命令执行此查询: cursor.execute(insert_enquiry) 我得到以下错误: MySQL Error [106

我的要求是检查表中是否有与列的值列表匹配的条目。我正在使用以下查询:

insert_enquiry =  """SELECT P_ID,Jobname FROM build_table WHERE Build_number IN %s;""" % 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
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)
这将导致以下错误:

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
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_约束。

多亏了

其想法是将
len(insert_constraint)
number of
%s
添加到查询字符串中。这样,查询将接受
WHERE IN
子句的列表

insert_enquiry =  "SELECT build_number,jobname FROM %s WHERE build_number IN (%s);"
insert_enquiry_string =', '.join(map(lambda x: '%s', insert_constraint_build_num))
insert_enquiry = insert_enquiry % (table_name, insert_enquiry_string)
cursor.execute(insert_enquiry, insert_constraint_build_num)

这就解决了问题。

不是用(…)而不是[…]吗?是的,我已经对问题进行了编辑。它以某种方式获取列表中的最后一个元素insert_约束,并跳过列表中的所有元素。