python:如何将参数传递给 SQL 查询

python: How to pass parameter into SQL query

我有一个带有参数的函数。此参数必须在 SQL 查询中替换,然后由 pandasql 执行。这是我的功能:

def getPolypsOfPaitentBasedOnSize(self,size):


   smallPolypQuery = """ 
                       select *
                       from     polyp 
                       where polyp.`Size of Sessile in Words` ==  """ +size

   smallPolyps = ps.sqldf(smallPolypQuery)

当我 运行 代码时,出现以下错误:

    raise PandaSQLException(ex)
pandasql.sqldf.PandaSQLException: (sqlite3.OperationalError) no such column: Small
[SQL:  
                       select *
                       from     polyp 
                               where polyp.`Size of Sessile in Words` ==  Small]

看来,我必须以某种方式让它像

 where polyp.`Size of Sessile in Words` == 'Small'

但是,我不知道该怎么做!

更新:

我已经厌倦了下面的解决方案,也没有错误,但查询没有 return 任何东西

""" 
    select *
    from     polyp 
    where polyp.`Size of Sessile in Words` ==  " """ +size+ """ " """

我确信(如果 size="Small")下面的语句对我有用:

where polyp.`Size of Sessile in Words` ==  "Small"

format可以用

size = 'Small'

smallPolypQuery = """ 
select *
from   polyp 
where  polyp.`Size of Sessile in Words` ==  {0}""".format(size)

print(smallPolypQuery)

结果是:

select *
from   polyp 
where  polyp.`Size of Sessile in Words` ==  Small

如果您需要报价,请将其放入 smallPolypQuery,例如:

smallPolypQuery = """ 
select *
from   polyp 
where  polyp.`Size of Sessile in Words` ==  "{0}" """.format(size)