在 to_date 函数中使用时,无法使 cx_oracle 变量正确绑定

Cannot get cx_oracle variable to bind properly when using it in to_date function

我正在尝试替换此调用:

cursor = connection.cursor()
try:
    sql = "select m.lat, m.longt, v.tmax from vcsn_view v, vcsn_metadata m where set_vcsn('local_day=to_date(20180304)-1') is null and v.agent_no = m.agent_no(+) order by 1,2"
    cursor.execute(sql)
    for lat, longt, tmax in cursor:
        print("Values:", lat, longt, tmax)
except cx_Oracle.DatabaseError, e:
    printf ('Failed to select\n')
    printException(e)
    exit(1)

调用将查询中的日期作为变量,即:

cursor = connection.cursor()
try:
    sql = "select m.lat, m.longt, v.tmax from vcsn_view v, vcsn_metadata m where set_vcsn('local_day=to_date(:1)-1') is null and v.agent_no = m.agent_no(+) order by 1,2"
    cursor.execute(sql, (20180304,))
    for lat, longt, tmax in cursor:
        print("Values:", lat, longt, tmax)
except cx_Oracle.DatabaseError, e:
    printf ('Failed to select\n')
    printException(e)
    exit(1)

但我无法让它工作。我收到错误:
未能 select 错误代码 = 1036 错误信息 = ORA-01036: 非法变量 name/number

在您的 SQL 中有 :1 内引号,因此它不起作用。我不确定 set_vcsn() 是否需要字符串。假设确实如此,您需要执行 following:cursor = connection.cursor()

try:
    sql = "select m.lat, m.longt, v.tmax from vcsn_view v, vcsn_metadata m where set_vcsn('local_day=to_date(' || :1 || ')-1') is null and v.agent_no = m.agent_no(+) order by 1,2"
    cursor.execute(sql, (20180304,))
    for lat, longt, tmax in cursor:
        print("Values:", lat, longt, tmax)
except cx_Oracle.DatabaseError, e:
    printf ('Failed to select\n')
    printException(e)
    exit(1)

如果 set_vcsn() 不需要字符串,那么如果您需要进一步的帮助,提供更多信息会很有帮助。但一般情况下,不会搜索字符串中的值来查找绑定变量!