简体   繁体   English

我如何在python sql中传递值列表?

[英]How i can pass a list of values in python sql?

I am passing the list of values in IN query. 我在IN查询中传递值列表。 When I run this query in workbench and other places it is working but not working inside the python code. 当我在工作台和其他地方运行此查询时它正在工作但不在python代码中工作。

In python, it is taking values as a list so it is giving an error. 在python中,它将值作为列表,因此它给出了一个错误。

I want to pass a list of values in a Python SQL query. 我想在Python SQL查询中传递值列表。 But it gives me an error: 但它给了我一个错误:

can only concatenate str (not "tuple") to str 只能将str(不是“元组”)连接到str

I also try with the list but then it gives an error: 我也尝试使用列表但是它给出了一个错误:

can only concatenate str (not "list") to str 只能将str(不是“list”)连接到str

emp_id = ['470224', '471822', '470551', '471854', '471603', '468121', '458435', '466700', '471605', '471609', '471604', '458535', '454387', '469881']

mycursor.execute("select * from table_name where id in "+"'"+emp_id+"'")

The query must work as follows: 查询必须如下工作:

select * from table_name where id in ('470224', '471822', '470551', '471854', '471603', '468121', '458435', '466700', '471605', '471609', '471604', '458535', '454387', '469881') ;

It must fetch all the record which is in the IN query. 它必须获取IN查询中的所有记录。

emp_id = ['470224', '471822', '470551', '471854', '471603', '468121', '458435', '466700', '471605', '471609', '471604', '458535', '454387', '469881']
mycursor.execute("select * from table_name where id in " + str(tuple(emp_id)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM