如何将变量(Date)作为python中的参数传递给SQL?

时间:2015-07-05 18:07:29

标签: python

请您指导我如何在python脚本中将变量(日期)作为参数传递给下面的SQL(Postgre SQL数据库)?

last_Execution_date = datetime.datetime.now().strftime("%Y-%m-%d")

查询:

SELECT DISTINCT
   researcherid,
   username,
   firstname,                                                      
   lastname,                                                         
FROM researcher r
  INNER JOIN principals p ON p.researcherid = r.researcherid  
WHERE r.lastmodifieddate > 'last_Execution_date'

我需要将last_Execution_date作为参数传递给上面的SQL语句。有人可以告诉我正确的语法吗?我不太确定是否可以将外部变量作为参数传递,因为SQL语句在数据库上运行并返回数据。

1 个答案:

答案 0 :(得分:0)

尝试类似:

说:

  last_Execution_date = datetime.datetime.now()<br>

然后,

cursor.execute('SELECT DISTINCT researcherid , username, firstname,
lastname,FROM researcher r INNER JOIN principals p 
ON p.researcherid = r.researcherid 
WHERE r.lastmodifieddate > %s',last_Execution_date)