简体   繁体   中英

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

I am passing the list of values in IN query. When I run this query in workbench and other places it is working but not working inside the python code.

In python, it is taking values as a list so it is giving an error.

I want to pass a list of values in a Python SQL query. But it gives me an error:

can only concatenate str (not "tuple") to str

I also try with the list but then it gives an error:

can only concatenate str (not "list") to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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