【问题标题】:How to prevent ORA-00900 error when running Oracle SQL Developer query using pandas.read_sql()?使用 pandas.read_sql() 运行 Oracle SQL Developer 查询时如何防止 ORA-00900 错误?
【发布时间】:2020-10-29 19:08:49
【问题描述】:

下面是我想从 Python 运行的 SQL Developer 查询示例。

sqlQuery = """
DEFINE a = 1
DEFINE b = 2

select &&a + &&b from dual
"""
result = pandas.read_sql(sql=sqlQuery, con=Oracle_Connection) 

当我在 SQL Developer 中运行此查询时,它有效,但是当我在 Python 中运行上述代码时,我收到以下错误:'ORA-00900: invalid SQL statement'。有什么简单的方法可以解决这个问题吗?

【问题讨论】:

    标签: python sql oracle oracle-sqldeveloper


    【解决方案1】:
    DEFINE a = 1
    DEFINE b = 2
    

    DEFINE 是 SQL*Plus 或 SQLDeveloper 的命令,不是 SQL 命令。 您需要改用绑定变量:

    select :a + :b from dual;
    

    完整示例:

    sqlQuery = """
    select :a + :b from dual
    """
    result = pandas.read_sql(sql=sqlQuery, con=Oracle_Connection, params={'a': 1, 'b': 2})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      相关资源
      最近更新 更多