简体   繁体   English

cx_Oracle 'SELECT INTO' 绑定变量问题 - ORA-01036:非法变量名称/编号

[英]cx_Oracle 'SELECT INTO' bind variable issue - ORA-01036: illegal variable name/number

I am getting an "illegal variable name" error with the code below.下面的代码出现“非法变量名”错误。 One is an out parameter, not sure if this syntax works with out parameters, but have also tried a few variations incluing passing a tuple of values in with:1, :2 syntax - that doesn't work either.一个是 out 参数,不确定此语法是否适用于 out 参数,但也尝试了一些变体,包括在 with:1, :2 语法中传递值的元组 - 这也不起作用。

def no_snapshot_for_day(timestamp):
    # Do we have a snaphot already today
    no_snapshot = True
    snapshot_count = cursor_analytics.var(cx_Oracle.NUMBER)

    sql = "SELECT COUNT(1) INTO :v1 FROM PYTHON_SNAPSHOT WHERE SNAPSHOT_DATE=trunc(:v2) AND SNAPSHOT_STATUS='OK'"

    cursor_analytics.execute(sql,{"v1":snapshot_count, "v2":timestamp})


    if snapshot_count.getvalue() > 0:
        no_snapshot = False

    return no_snapshot

'select into' is a PL/SQL SQL syntax. 'select into' 是 PL/SQL SQL 语法。 Technically you can execute从技术上讲,您可以执行

sql = "BEGIN SELECT COUNT(1) INTO :v1 FROM PYTHON_SNAPSHOT WHERE SNAPSHOT_DATE=trunc(:v2) AND SNAPSHOT_STATUS='OK'; END;"

but in practice, just do a 'select' and then fetch the data, something like this (untested) code:但在实践中,只需执行“选择”然后获取数据,就像这样(未经测试的)代码:

with connection.cursor() as cursor_analytics:
    sql = "SELECT COUNT(1) as snapshot_count FROM PYTHON_SNAPSHOT WHERE SNAPSHOT_DATE=trunc(:v2) AND SNAPSHOT_STATUS='OK'"
    cursor_analytics.execute(sql,{"V2": timestamp})
    (snapshot_count,) = cursor_analytics.fetchone()
    print(snapshot_count)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM