python list in sql query as parameter

后端 未结 15 1454
耶瑟儿~
耶瑟儿~ 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条回答
  •  -上瘾入骨i
    2020-11-22 09:51

    For example, if you want the sql query:

    select name from studens where id in (1, 5, 8)
    

    What about:

    my_list = [1, 5, 8]
    cur.execute("select name from studens where id in %s" % repr(my_list).replace('[','(').replace(']',')') )
    

提交回复
热议问题