python list in sql query as parameter

后端 未结 15 1448
耶瑟儿~
耶瑟儿~ 2020-11-22 09:39

I have a python list, say l

l = [1,5,8]

I want to write a sql query to get the data for all the elements of the list, say

s         


        
15条回答
  •  悲哀的现实
    2020-11-22 10:05

    Dont complicate it, Solution for this is simple.

    l = [1,5,8]
    
    l = tuple(l)
    
    params = {'l': l}
    
    cursor.execute('SELECT * FROM table where id in %(l)s',params)
    

    I hope this helped !!!

提交回复
热议问题