python list in sql query as parameter

后端 未结 15 1727
耶瑟儿~
耶瑟儿~ 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 09:46

    placeholders= ', '.join("'{"+str(i)+"}'" for i in range(len(l)))
    query="select name from students where id (%s)"%placeholders
    query=query.format(*l)
    cursor.execute(query)
    

    This should solve your problem.

提交回复
热议问题