【问题标题】:bind parameters to dynamically update the table name绑定参数动态更新表名
【发布时间】:2021-08-25 22:50:25
【问题描述】:

我正在尝试为 Oracle 数据库执行 select 语句。在我的 select 语句中,我不需要直接给出表名,而是需要从表名列表中检索它,只更新指定部分并在查询中给出它。

尝试了一些代码,但最终出现错误...请在下面找到我的代码

tempList = ['a', 'b', 'c', 'd']


"Select * from db.vw_fs_{}_all".format(tempList[i])

我试过了,

query = "Select * from db.vw_fs_:name_all"

conn = cx_Oracle.connect(user = uid, password = pwd)
cur = conn.cursor()
cur.execute(query, {'name': tempList[0]})

它给了我 ORA-00903: invalid table name 错误。

谁能给我一些技巧来在oracle for python环境中完成动态绑定参数?

【问题讨论】:

  • 我也试过:object_name = Cursor.callfunc('sys.dbms_assert.sql_object_name' , cx_Oracle.STRING, ['name']) 但我得到 ORA-06550: line 1, column 13 :PLS-00306:调用“SQL_OBJECT_NAME”时参数的数量或类型错误 ORA-06550:第 1 行,第 7 列:PL/SQL:语句被忽略错误。

标签: python sql dynamic bind cx-oracle


【解决方案1】:

很遗憾,您不能使用绑定变量来指定表名(或列名)。你必须这样做:

temp_list = ['a', 'b', 'c', 'd']
for suffix in temp_list:
    query = f"select * from db.vw_fs_{suffix}_all"
    cursor.execute(query)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多