Cx_oracle:To_date 变量在 python

Cx_oracle: To_date with variables in python

我正在尝试这样做

startdate = "20160123"
enddate = "20160204"
cmd = "select identification_number from bug where submitted_date >= TO_DATE(:1,'dd-MON-yy') and submitted_date <= TO_DATE(:2,'dd-MON-yy')"
cursor.execute(cmd,(startdate,enddate))

我收到一个错误

cursor.execute(cmd,(sdate,edate))
cx_Oracle.DatabaseError: ORA-01861: literal does not match format string

我看到了关于此错误的先前线程,但没有解决我的问题

我不确定 startdateenddate 是如何转换为 :1:2 但如果是这样,那就是日期格式的问题。 您正在传递 YYYYMMDD 并将其转换为 DD-MON-YYYY 。尝试改变它。 您还缺少 from 子句。

我使用了 between 子句。

select identification_number 
from <your_table>
where 
submitted_date between 
TO_DATE(:1,'YYYYMMDD') and TO_DATE(:2,'YYYYMMDD')

如果可行,请在您的代码中使用相同的日期格式

startdate = "20160123"
enddate = "20160204"
cmd = "select identification_number from <your_table> where submitted_date between TO_DATE(:1,'YYYYMMDD') and TO_DATE(:2,'YYYYMMDD')"